Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

RegEx question

by mohamedrabeek (Initiate)
on Jul 23, 2010 at 12:25 UTC ( #851026=perlquestion: print w/replies, xml ) Need Help??

mohamedrabeek has asked for the wisdom of the Perl Monks concerning the following question:

Hello I would like to have a regex which matches anything but the line ending with comma followed by 0 or more spaces. Please test it before giving me the solution. I initially thought it is easy and later felt difficult to solve it. Example:
1) this is an example - should match 2) this is an example, - should not match 3) this is an example, - should not match 4) this is an example - should match
Thanks, Mohamed Guys, The solutions you provided is using the control features from PERL language to match the other lines and isolate the required ones in the else block. I just wanted to have the regex matching in the if block itself and not in the else block. For instance, we can check our regex in this website to see its validness. I can think of a solution which is close to Moritz solution. My regex is ^(?!.*,\s*$) The ?! is used for negative lookahead. Do you guys think of any other alternatives to achieve this. Please don't use the programming construct. Just focus on the REGEX alone. Thanks & Regards, Mohamed

Replies are listed 'Best First'.
Re: RegEx question
by moritz (Cardinal) on Jul 23, 2010 at 12:31 UTC
    Please test it before giving me the solution.

    Is this another "please do the work for me" thing? If you want good answers, a nice way is to provide the testing code yourself, so that people just need to plug in their regexes.

    Anyway, my approach would be /^(?!.*, $)/

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: RegEx question
by roboticus (Chancellor) on Jul 23, 2010 at 12:33 UTC

    mohamedrabeek:

    Hint: It would be easy to match all other lines. You can get that one, right?

    If not....

    ...roboticus

Re: RegEx question
by zek152 (Pilgrim) on Jul 23, 2010 at 12:48 UTC

    Show what you used so we can help you learn why it did not work. The code below shows a working solution. It is not very elegant but it does work:

    $line1 = "this is an example"; $line2 = "this is another example,"; $line3 = "this is yet another example "; $line4 = "despite what you think this is not an example, "; if ($line1 !~ /.*,\s*$/) { print "MATCHED : $line1\n"; } else { print "MISMATCH: $line1\n"; } # ... repeated for $line2 $line3 and $line 4 ########################################################## # Output #MATCHED : this is an example #MISMATCH: this is another example, #MATCHED : this is yet another example #MISMATCH: despite what you think this is not an example, ##########################################################
Re: RegEx question
by ahmad (Hermit) on Jul 23, 2010 at 12:54 UTC

    Like this?

    while (<DATA>) { if (!/,\s+$/) { print; } } __DATA__ 1) this is an example 2) this is an example, 3) this is an example, 4) this is an example
Re: RegEx question
by japhy (Canon) on Sep 21, 2010 at 19:29 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://851026]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2020-07-04 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?