Reputation: 7633
I want to match 'windowsXP' in this string:
windowsXP|7|8
Or
windowsXP 7 8
I wrote a pattern to match it:
([^0-9|])+[0-9\.]+.*
Why doesn't it match? It's the | making the trouble in the bracket. I tried escaping it but it didn't work either.
([^0-9\|])+[0-9\.]+.*
Upvotes: 2
Views: 60
Reputation: 18611
Look:
([^0-9|])+
find characters different from digits and pipes[0-9\.]+
attempts matching a digit or a period, but there is a pipe that the expression cannot catch.Use
^([^|\s]+)[|\s](\d+)[|\s](\d+)
See proof
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[^|\s]+ any character except: '|', whitespace (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
[|\s] any character of: '|', whitespace (\n, \r,
\t, \f, and " ")
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
[|\s] any character of: '|', whitespace (\n, \r,
\t, \f, and " ")
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \3
import re
regex = r"^([^|\s]+)[|\s](\d+)[|\s](\d+)"
test_str = "windowsXP|7|8"
print(re.findall(regex, test_str))
Results: [('windowsXP', '7', '8')]
Upvotes: 2
Reputation: 784998
You may use this regex to math both cases:
^[^|\s]+
RegEx Details:
^
: Start[^|\s]+
: Match 1+ of any character that are not |
and not whitespaceUpvotes: 1
Reputation: 41
It is rather simple. Just think of selecting the alphabets not negating numerics.
regex = \[a-z]*\i
Upvotes: -1