Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Regex \. help

by codz67 (Initiate)
on Nov 21, 2014 at 23:57 UTC ( #1108062=perlquestion: print w/replies, xml ) Need Help??

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

Oh great and wise monks, I beseech your help. I have this regex: ^regex*\.0-9 It matches regex.1 It must also match regex, but not regex.abc. . It seems as though the ? doesnt make the \. optional. Please help.
#!/usr/bin/perl @filename = `ls | grep "^regex\.[0-9]"`; print @filename; ...

prints regex.1. I need it to print regex and regex.1:

#!/usr/bin/perl @filename = `ls | grep "^regex\.?[0-9]"`; print @filename;

Doesnt print anything.

Shouldnt the ? match the . zero or more times? If so why doesnt it print regex and regex.1 As for the .abc I need that to be excluded when it greps. So regex.abc and regex.xyz wont print.

Thank you for the help so far, I am still stuck on making this work. I will continue to try to make this work, if anyone could assist further I would be very thankful.

Replies are listed 'Best First'.
Re: Regex \. help
by AnomalousMonk (Bishop) on Nov 22, 2014 at 00:14 UTC
    I have this regex: ^regex*\.0-9

    You do not use code tags, so it's not clear what regex you have. Please see Markup in the Monastery, Writeup Formatting Tips and How do I post a question effectively?.

    I going to guess that your regex is  /^regex*\.[0-9]/ You say that your regex matches "regex.1", which I can accept, but why would you think it would match "regex.abc", since "abc" has no relation to  [0-9] (a decimal digit)? Please see perlre and perlretut.

    It seems as though the ? doesnt make the \. optional.

    I don't see any  ? metacharacter. (Update: And what difference would you expect it would make if the  \. were optional?)

    Update: When I first read "It must also match regex, but not regex.abc." in the OP, I thought codz67 wanted the regex also to match  'regex.abc' and was puzzled why it didn't. A more careful reading brings a better understanding. See discussion below. Oh, well...

Re: Regex \. help
by Loops (Curate) on Nov 22, 2014 at 00:19 UTC

    Something using zero width negative look ahead (?!) should work:

    my @t = qw( regex regex.1 regex.12 regex.1a regex.a regex.abc regex.a1 + oregex.2 ); for (@t) { print $_; print ' matches!' if /^regex(?!\.[^\d])(\.\d*)?/; print "\n"; }
    gives:
    regex matches! regex.1 matches! regex.12 matches! regex.1a matches! regex.a regex.abc regex.a1 oregex.2

    Checkout the "Look-Around Assertions" section in the perlre documentation. In the example above, the negative look-ahead says fail if the next characters are a period followed by anything that isn't a digit.

      Loops: You read the OP a bit more slowly and carefully, and came closer, I think, to answering codz67's question as a result. But the  (\.\d+)? is redundant for the given inputs and outputs if the negative look-ahead is used. (Had it been needed, it would probably have been better as a  (?:...) non-capturing group.)

      c:\@Work\Perl>perl -wMstrict -le "my @t = qw( regex regex.1 regex.12 regex.1a regex.a regex.abc regex.a +1 oregex.2 ); for (@t) { printf qq{'$_'}; printf ' matches!' if /^regex(?!\.[^\d])/; print ''; } " 'regex' matches! 'regex.1' matches! 'regex.12' matches! 'regex.1a' matches! 'regex.a' 'regex.abc' 'regex.a1' 'oregex.2'

        Hi,

        Yeah you're right about that. I confess to removing the non-capturing annotation for simplicity sake. Here's a slightly different version where the \d* isn't redundant:

        my @t = qw( regex regex. regex.1 regex.12 regex.1a regex.a regex.abc r +egex.a1 oregex.2 ); say s/^regex(?!\.[^\d])(\.\d*)?/|$&|/r for (@t);
        |regex| |regex.| |regex.1| |regex.12| |regex.1|a regex.a regex.abc regex.a1 oregex.2

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2022-09-28 18:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (124 votes). Check out past polls.

    Notices?