Sunday 7 January 2018

pcre - Regex pattern to match string that's not followed by a colon

itemprop="text">

Using regex, I'm trying to match any
string of characters that meets the following conditions (in the order
displayed):





  • Contains
    a dollar sign $; then

  • at least
    one letter [a-zA-Z]; then

  • zero
    or more letters, numbers, underscores, periods (dots), opening brackets, and/or closing
    brackets [a-zA-Z0-9_.\[\]]*;
    then

  • one pipe character |;
    then

  • one at sign @;
    then

  • at least one letter
    [a-zA-Z]; then

  • zero or more
    letters, numbers, and/or underscores [a-zA-Z0-9_]*;
    then

  • zero colons
    :




In
other words, if a colon is found at the end of the string, then it should
not count as a
match.



Here are some examples of valid
matches:



$tmp1|@hello
$x2.h|@hi_th3re
Valid
match$here|@in_the middle of other
characters



And
here are some examples of invalid
matches:



$tmp2|@not_a_match:"because
there is a
colon"
$c.4a|@also_no_match:


Here
are some of the patterns I've
tried:



(\$[a-zA-Z])([a-zA-Z0-9_.\[\]]*)(\|@)([a-zA-Z][a-zA-Z0-9_]*(?!.[:]))
(\$[a-zA-Z])([a-zA-Z0-9_.\[\]]+)?(\|@)([a-zA-Z][a-zA-Z0-9_]*(?![:]))

(\$[a-zA-Z])([a-zA-Z0-9_.\[\]]+)?(\|@)([a-zA-Z][a-zA-Z0-9_]*)([^:])


Answer




This pattern will do what you
need



\$[A-Za-z]+[\w.\[\]]*[|]@[A-Za-z]+[\w]*+(?!:)


href="https://regex101.com/r/fX2uY2/4" rel="nofollow">Regex
Demo




I am
using possessive quantifiers to cut down the backtracking using
[\w]*+. You can also use atomic groups instead of possessive
quantifiers
like



\$[A-Za-z]+[\w.\[\]]*[|]@[A-Za-z]+(?>[\w]*)(?!:)


NOTE





\w =>
[A-Za-z0-9_]





No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...