The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

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

  • 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
  • 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
  • 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
  • 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
  • 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
  • LewishamLewisham Registered User regular
    edited November 2007
  • 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
  • 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.