Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using anchors in python regex to get exact match

Tags:

python

regex

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

like image 569
astrogirl Avatar asked Oct 24 '11 17:10

astrogirl


People also ask

How do you find the exact match in Python?

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.

How do anchors work in regex?

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.

How do you specify an end in regex?

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.


3 Answers

Pretty straightforward. First, put anchors on your pattern:

"^patternhere$"

Now, let's put together the pattern:

"^v\d+$"

That should do it.

like image 176
Stephen Gross Avatar answered Oct 04 '22 22:10

Stephen Gross


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.

like image 20
brianary Avatar answered Oct 04 '22 22:10

brianary


Simply use

 \bv\d+\b

Or enclosed it with ^\bv\d+\b$

to match it entirely..

like image 22
FailedDev Avatar answered Oct 04 '22 22:10

FailedDev