LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-24-2012, 09:00 AM   #1
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Rep: Reputation: 78
PCRE Validation


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.
 
Old 06-25-2012, 02:35 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
http://www.regextester.com/
 
Old 06-25-2012, 05:19 AM   #3
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by acid_kewpie View Post
nice site, how does it work? i put in my regex and raw text, then what?
 
Old 06-26-2012, 12:37 AM   #4
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by Linux_Kidd View Post
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>
 
Old 06-26-2012, 06:25 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by Linux_Kidd View Post
nice site, how does it work? i put in my regex and raw text, then what?
then you read the "result" line. Not exactly complex.
 
Old 06-26-2012, 09:43 PM   #6
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
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.
 
Old 06-28-2012, 01:11 AM   #7
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by Linux_Kidd View Post
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.
 
Old 06-28-2012, 05:29 AM   #8
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by Diantre View Post

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.
 
Old 06-28-2012, 08:34 PM   #9
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by Linux_Kidd View Post
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...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] PCRE Question MTK358 Programming 11 06-01-2011 11:12 AM
Examples of PCRE library in C Centinul Programming 11 09-27-2010 05:13 AM
PCRE problems - how to get the info vargadanis Programming 1 07-15-2008 04:33 AM
How to use PCRE in C ? raiux Programming 1 11-23-2007 06:43 AM
PCRE Regex Osiris990 Programming 10 10-22-2007 02:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration