Re: Expressing AND, OR, and NOT in a Single Pattern



On Mar 1, 3:18 pm, "h3xx" <amphetamach...@xxxxxxxxx> wrote:
I like doing things in one line:

print grep { /suspended/ && ! /Data_services/ } <DATA>;


I prefer this method too. For clarity and long-term maintenance it is
much better because the esoterica of regex can make the desired
results hard to figure out and the bugs in the pattern even harder to
find.

Also, speed wise, this is a lot faster. The regex engine has to do a
lot of work that can be short circuited by the booleans.

Sometimes it's better to break the search for matching patterns into
single lines too. It's kind of macho programmer-wise to string it all
together into one mondo regex pattern and have it work, but the logic
can get fragile.

The only thing I'd do differently to these patterns is add an anchor
to the 'Data_services' pattern, like so...

/^<Query id='Data_services/

Anchors speed up regex an incredible amount. I did benchmarks of index
vs various ways of using regex, and an anchored qr// that was
initialized outside a loop was the fastest at finding patterns inside
long strings, when the pattern was at the end of the string. At the
beginning of a string it should be equal to index(). Index() was
faster when finding a fixed string somewhere in the middle of another
string.

.



Relevant Pages

  • Re: Regex question
    ... structure of the date you're trying to extract. ... For example, in Regex you can ... pattern that will ensure a valid date within the range allowed by T-SQL ... valid date from a string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: regex
    ... case sensitivity was part of the problem which I fixed with ... In other word I got syntax problem with the month pattern ... >>against the articles and help on regex, I still can't find the mistake I ... >> Public Function regtest(ByVal StringIn As String, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How do I shorten or split this function
    ...    (scan regex string) ... Instead of field-name you mean field-type. ... it doesn't test the string to see if it's of the suitable form for the ... corresponding field of the pattern. ...
    (comp.lang.lisp)
  • Re: Interaction between two strings
    ... then the string XYZC should give a match. ... > string that is supplied separately from the regex. ... $patterns, you splice them all into one very big $pattern. ... Doing this is immensely faster than iterating through an array or the like: ...
    (comp.lang.perl.misc)
  • Re: regex question
    ... have exactly 3 digits not longer. ... so you do want to anchor at the beginning and end of the string. ... should show you how to anchor a regex to the start and end of a string. ...
    (perl.beginners)

Loading