Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

About regular expressions

by nagalenoj (Friar)
on Mar 31, 2009 at 04:46 UTC ( #754318=perlquestion: print w/ replies, xml ) Need Help??
nagalenoj has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I have tried a simple regular expression like,

^[a-zA-Z]+(['.]?\s*[a-zA-Z]+)*$
to match the following text lines,
St.John's high school. A. B. C. school. Institute of management. oxford university. A.B.C college. Ragu ram's college of technology.
And at the same time, it chould not match the following lines
A..B.C college. St.John''s high school. St.John'.'s high school.
The given regular expression is working fine.

But, my question is, is there any other better regex which will work instead of the given one?
And if any other better way, kindly tell me how this is worse than that.
Will a poor regex make a big difference in performance of a program?
How can we validate a efficiency of a regular expressions?

Ofcourse, I have searched in google to some extent.

Comment on About regular expressions
Select or Download Code
Re: About regular expressions
by CountZero (Bishop) on Mar 31, 2009 at 05:49 UTC
    is there any other better regex which will work instead of the given one?
    Of course, "There Is More Than One Way To Do It"!

    It all depends how you define your matches. It seems you want to reject all lines with a sequence of

    1. two (or more) single quotes
    2. two (or more) full stops
    3. single quote, full stop
    4. full stop, single quote
    5. ...
    In other words any sequence of full stops and single quotes without other characters in between.

    You can therefore search for this rejection pattern and accept all lines which are not matched by the rejection pattern.

    use strict; use warnings; while (<DATA>) { if (/['.]{2,}/) { print "Rejected: $_"; } else { print "Accepted: $_"; } } __DATA__ St.John's high school. A. B. C. school. Institute of management. oxford university. A.B.C college. Ragu ram's college of technology. A..B.C college. St.John''s high school. St.John'.'s high school.
    Is this regex better? That --again-- depends. If your "match" pattern is rather complicated or the strings to match are long, it can take a while to run and sometimes the "rejection" pattern is simpler as it will stop as soon as one of the rejection cases is found.

    In case of doubt, just profile your code and see what performs best.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: About regular expressions
by eye (Chaplain) on Mar 31, 2009 at 06:18 UTC
    First, I'm assuming you don't want the terminal periods to be matched. You want to match:
    St.John's high school
    not:
    St.John's high school.

    I want to be cautious in answering this because the specification is just a series of examples. It's very possible that I do not actually understand the pattern you are trying to match. That said, I don't see a way to materially improve the performance of your regex. I can say with certainty that a poorly defined regex can adversely affect the performance of a program. Sadly, I do not know of a way to validate the efficiency of a regex.

    The only vaguely useful suggestion I can offer is to use [a-z] in a case insensitive match instead of [a-zA-Z]. That makes it slightly easier to read, but has no affect on how the regex would perform.

Re: About regular expressions
by AnomalousMonk (Monsignor) on Apr 01, 2009 at 00:18 UTC
    The given regular expression is working fine.
    I do not understand this assertion.
    >perl -wMstrict -le "my $rx = qr{^[a-zA-Z]+(['.]?\s*[a-zA-Z]+)*$}xms; print '--- output --'; for (@ARGV) { print m{$rx} ? ' ' : 'NO', ' match: ', $_; } " "all of the following should match" "FooBar Tech" "St.John's high school." "A. B. C. school." "Institute of management." "oxford university." "A.B.C college." "Ragu ram's college of technology." "none of the following should match" "A..B.C college." "St.John''s high school." "St.John'.'s high school." --- output -- match: all of the following should match match: FooBar Tech NO match: St.John's high school. NO match: A. B. C. school. NO match: Institute of management. NO match: oxford university. NO match: A.B.C college. NO match: Ragu ram's college of technology. match: none of the following should match NO match: A..B.C college. NO match: St.John''s high school. NO match: St.John'.'s high school.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-07-30 00:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls