Solved perl regex

csptra

Member


Messages: 54

Hello,

I admit, I am not proficient with regex. I'm trying to match a pattern taken from the output of reformime. (see below). I am not having much luck. When I can get it to work, then it matches everything. I think this is due to the working pattern matching the longer pattern.

Sample input:

Code:
1
1.1
1.1.1
1.1.1.1
1.1.1.2
1.1.2
1.2
1.2.1
1.2.1.1
1.2.1.2
1.3
1.3.1
1.3.1.1
1.3.1.2
1.4
1.4.1
1.5
1.5.1
1.5.1.1
1.5.1.2
1.6
1.6.1
1.6.1.1
1.6.1.2
1.7
1.7.1
1.7.1.1
1.7.1.2
....
1.29
1.29.1
1.29.1.1
1.29.1.2
....

the output can be arbitrary in length. (from 1.2 down to however many attachments there are). My goal is to match a single digit followed by a period followed by the next to digits. I don't want to match anything beyond that.

I want to match any of the following:

1.1
1.2
1.6
...
1.29

examples of what I don't want to match:
1.2.1
...
1.29.1
1.29.1.1
(basically any of the 'sub' numbers)


Please let me know if this doesn't make sense.

I have tried various forms the following regex
\d\.\d{2}
.\...

and I found something like this on the internet

(?<!\d)\d{1}\.\d{2}(?!\d)

and tried to modify it to fit my needs, but didn't work.

Any help would be appreciated.
 

usdmatt

Daemon

Reaction score: 607
Messages: 1,545

If you just want to match lines that have a digit, dot, then more digits, try the following

^\d\.\d{1,2}$

Code:
^ - match from the start of the line
\d - single digit
\. - dot
\d{1,2} - one or two digits
$ - match the end of the line

If you want to match the first two numbers of lines that contain more than two numbers (e.g. match "1.2" from "1.2.3.4"), just take the dollar sign out so the pattern isn't looking for the line ending right after the second number.
 
OP
csptra

csptra

Member


Messages: 54

Thanks, that makes sense, however, I'm trying to figure out how to use it properly.

Even though I used the \d (in my op) .. I'f I understand what I researched correctly, I've read that \d is a perl regex. The man page of grep shows -P for perl extensions, but says on FreeBSD, it is unsupported.

Code:
man grep
 -P, --perl-regexp
              Interpret PATTERN as a Perl regular expression.  This option  is
              not supported in FreeBSD.

I've also read that since it is a perl extension, it can't be used with grep -E or -e.

I've tried to use perl to get it to match, but my syntax is probably wrong, I found this code at http://www.rexegg.com/regex-perl-one-liners.html


Code:
perl -ne 'print if /regex_goes_here/'

so I tried

Code:
cat email | reformime | perl -ne 'print if /^\d.\d{1.2}$/'

and it returns nothing. I also tried

Code:
perl -ne 'print if /^\d.\d{1.2}$/' test

after dumping the output of reformime to a file called test
 

wblock@

Beastie Himself
Developer

Reaction score: 3,753
Messages: 13,851

In regular expressions, dot matches any character unless you escape it. So ^\d\.. I don't know what {1.2} is, maybe you meant {1,2}.
 
OP
csptra

csptra

Member


Messages: 54

Thank you!!! that was the answer. I completely overlooked the typo. usdmatt's suggestion works perfectly.

for the record, the command is:
Code:
cat email | reformime | perl -ne 'print if /^\d.\d{1,2}$/'

I think my other problem (originally) was trying to escape the period. - thinking for a literal period. Well. I just tried the escape, and it works as well.
Code:
cat email | reformime | perl -ne 'print if /^\d\.\d{1,2}$/'
 
Top