ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
just looking for validation that my conversion to PCRE is correct.
i converted spaces to \s+
and i converted #'s to \d and where there were two digits or more i used \d{} syntax
Code:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0.1) Gecko/20100101 Firefox/10.0.1
PCRE
Mozilla/\d\.\d\s+\(Macintosh;\s+Intel\s+Mac\s+OS\s+X\s+\d{1,2}\.\d;\s+rv:\d{1,2}\.\d\.\d\)\s+Gecko/\d{1,8}\s+Firefox/\d{1,2}\.\d\.\d
does this look correct?
Last edited by Linux_Kidd; 06-24-2012 at 09:01 AM.
just looking for validation that my conversion to PCRE is correct.
If you have the PCRE package installed, you can use the pcretest program to test your regular expressions. Have a look at its manual page for more information.
Basically, this program asks for your regex with a re> prompt, then asks for the text with a data> prompt and reports if there's a match or not.
Your regular expression is fine, just had to escape the forward slashes to use them as delimiters (or you can leave the slashes as they are and use another character for delimeter, such as #):
Code:
PCRE version 8.12 2011-01-15
re> /Mozilla\/\d\.\d\s+\(Macintosh;\s+Intel\s+Mac\s+OS\s+X\s+\d{1,2}\.\d;\s+rv:\d{1,2}\.\d\.\d\)\s+Gecko\/\d{1,8}\s+Firefox\/\d{1,2}\.\d\.\d/
data> Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0.1) Gecko/20100101 Firefox/10.0.1
0: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0.1) Gecko/20100101 Firefox/10.0.1
data> something else
No match
data>
ok, the results just say whatever i type in for text, no mater what my RE looks like, hence i dont get the tool.
i am not asking to validate if my RE will match my sample text. i am asking for RE validation in syntax because i expanded the RE to catch more stuff, etc.
i am asking for RE validation in syntax because i expanded the RE to catch more stuff, etc.
The pcretest tool validates regex syntax as well. If I type an incorrect regex it will complain, for instance:
Code:
PCRE version 8.12 2011-01-15
re> /foo[bar/
Failed: missing terminating ] for character class at offset 7
re>
The regex you provided is a valid PCRE regular expression, it works fine with pcretest. The only thing to be aware of is that forward slashes have to be escaped if this character is used as the regex delimiter.
The regex you provided is a valid PCRE regular expression, it works fine with pcretest. The only thing to be aware of is that forward slashes have to be escaped if this character is used as the regex delimiter.
so if want forward slash to be a pattern char i need \/
is that what you mean?
i see some PHP documentation that says forward slashes need escaping. so PCRE in general needs / escaped with \ if the / is to be a literal char
Last edited by Linux_Kidd; 06-28-2012 at 05:34 AM.
so if want forward slash to be a pattern char i need \/
is that what you mean?
i see some PHP documentation that says forward slashes need escaping. so PCRE in general needs / escaped with \ if the / is to be a literal char
Yes, that's the idea. However, PCRE allows the use of other characters as delimiters, with the exception of alphanumerics and the backslash. For example, if we have a regular expression to match directory names, such as /dir1/.*/dir2, the forward slashes need to be escaped if the regex delimiter is also a forward slash:
Code:
/\/dir1\/.*\/dir2/
In these cases it's convenient to use another character as the regex delimiter to have a more legible regular expression, provided the delimeter doesn't appear in the regex, otherwise it must be escaped as well. The regex above can be written with # as the regex delimeter:
Code:
#/dir1/.*/dir2#
It's not necessary to escape the slashes and the regex is much easier to read...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.