As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

Quick Perl Regex Question

LoneIgadzraLoneIgadzra Registered User regular
edited November 2007 in Help / Advice Forum
How can I make a regular expression that counts up a certain number of non-adjacent non-whitespace characters?

An example: I want to make a regular expression that can find "devil" in "eden seer ever lion claw" by counting 4 non-whitespace characters between the letters, which do not have to be adjacent. (Another solution would be to remove whitespace from the string, but Perl apparently doesn't have a function for that.)

It seems like there ought to be a way, but, Perl tutorials and documentation are total ass and either barely touch on useful functionality or are completely incomprehensible.

LoneIgadzra on

Posts

  • Options
    Jimmy KingJimmy King Registered User regular
    edited November 2007
    If you want to remove whitespace from the string:

    my $string = 'eden seer ever lion claw';
    $string =~ s/\s+//g;

    pretty sure \s is spaces/tabs \S is non-space.

    The other way isn't going to happen, off the top of my head.

    Jimmy King on
  • Options
    falsedeffalsedef Registered User regular
    edited November 2007
    http://www.amazon.com/Perl-Language-Little-Black-Second/dp/1932111921

    I have it. One of my first language books.

    I don't use regex, though.

    You can trim white space by using:
    ^[ \t]+

    Then use something like:
    d....e....v....i....l

    falsedef on
  • Options
    LoneIgadzraLoneIgadzra Registered User regular
    edited November 2007
    Thanks a lot, that helped a great deal! I've been having a hard time finding a good explanation of s and /g all in one place, so that I could see that might work.

    LoneIgadzra on
  • Options
    Jimmy KingJimmy King Registered User regular
    edited November 2007
    falsedef: \t only matches tabs, I believe, so single spaces would be missed.

    Igadzra: In short, s/pattern 1/pattern 2/ pattern 2 in for the first instance of pattern 1 in your variable. adding the g on the end means "global", and so s/pattern 1/pattern 2/g will substitute pattern 2 in for every instance of pattern 1.

    Jimmy King on
  • Options
    LoneIgadzraLoneIgadzra Registered User regular
    edited November 2007
    Jimmy King wrote: »
    falsedef: \t only matches tabs, I believe, so single spaces would be missed.

    Igadzra: In short, s/pattern 1/pattern 2/ pattern 2 in for the first instance of pattern 1 in your variable. adding the g on the end means "global", and so s/pattern 1/pattern 2/g will substitute pattern 2 in for every instance of pattern 1.

    Yeah, I got how it worked after I looked it all up, and I'd already figured out what metacharacters I needed. Like I said, the pain in the ass was just finding a logical list of regex functions and modifiers in Perl, along with their respective "how-two's". (Searching the web for information on "/g" is a real bitch - hell even searching the perl docs page that that info is on is a pain in the ass, since a keyword search doesn't exactly work for a single character.) Anyway, thanks a lot!

    Update: Holy shit, my script takes much less time to search a 3 MB text file for "secret messages" than I would have thought.

    (You may think it's a silly script, and you'd be right, but it was a homework assignment, and online Perl documentation has been pissing me off all afternoon to the point I finally decided to ask someone - after the prof had most likely called it night. So I'm sorry to trouble you guys with this, and again thanks for the help!)

    LoneIgadzra on
  • Options
    LewishamLewisham Registered User regular
    edited November 2007
  • Options
    LoneIgadzraLoneIgadzra Registered User regular
    edited November 2007
    And again, my google-foo is found lacking.

    Edit: Wait, I'd already been to that page and accidentally closed it and forgot about it. :oops: I guess the lesson here is that Sam Adams is not conducive to efficient programming...

    Anyway, thanks - that's a bookmark for sure.

    LoneIgadzra on
  • Options
    falsedeffalsedef Registered User regular
    edited November 2007
    Jimmy King wrote: »
    falsedef: \t only matches tabs, I believe, so single spaces would be missed.

    There was a space in there, not just \t.

    falsedef on
Sign In or Register to comment.