I need to validate a version number consisting of 'v' plus positive int, and nothing else eg "v4", "v1004"
I have
import re
pattern = "\Av(?=\d+)\W"
m = re.match(pattern, "v303")
if m is None:
print "noMatch"
else:
print "match"
But this doesn't work! Removing the \A and \W will match for v303 but will also match for v30G, for example
Thanks
Exact match (equality comparison): == , != As with numbers, the == operator determines if two strings are equal. If they are equal, True is returned; if they are not, False is returned. It is case-sensitive, and the same applies to comparisons by other operators and methods.
Anchors are regex tokens that don't match any characters but that say or assert something about the string or the matching process. Anchors inform us that the engine's current position in the string matches a determined location: for example, the beginning of the string/line, or the end of a string/line.
End of String or Line: $ The $ anchor specifies that the preceding pattern must occur at the end of the input string, or before \n at the end of the input string. If you use $ with the RegexOptions. Multiline option, the match can also occur at the end of a line.
Pretty straightforward. First, put anchors on your pattern:
"^patternhere$"
Now, let's put together the pattern:
"^v\d+$"
That should do it.
I think you may want \b
(word boundary) rather than \A
(start of string) and \W
(non word character), also you don't need to use lookahead (the (?=...)
).
Try: "\bv(\d+)"
if you need to capture the int, "\bv\d+"
if you don't.
Edit: You probably want to use raw string syntax for Python regexes, r"\bv\d+\b"
, since "\b"
is a backspace character in a regular string.
Edit 2: Since +
is "greedy", no trailing \b
is necessary or desired.
Simply use
\bv\d+\b
Or enclosed it with ^\bv\d+\b$
to match it entirely..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With