Created
August 3, 2015 18:13
-
-
Save zubairalam/81023f756c4ec6bb6d90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def print_match(match): | |
if match is None: | |
print 'No match' | |
return | |
print match.group() | |
return match | |
string = "begin:id1:tag:id2:tag:id3:end" | |
print "\n{d}\n{s}\n{d}\n\n".format(s=string, d = '*'*len(string)) | |
'(?=...)' # Positive lookahead assertion | |
# Matches if '...' matches next. | |
print re.findall(r"(id\d+)(?=:tag)", string) | |
# ['id1', 'id2'] | |
'(?!...)' # Negative lookahead assertion | |
# Matches if '...' does not match next. | |
print re.findall(r"(id\d+)(?!:tag)", string) | |
# ['id3'] | |
'(?<=...)' # Positive lookbehind assertion | |
# Matches if the searched substring is preceded by a match for '...'. | |
# Note that the contained pattern must only match strings of some fixed length. | |
print re.findall(r"(?<=id\d:)(\b\w*\b)", string) | |
# ['tag', 'tag', 'end'] | |
'(?<!...)' # Negative lookbehind assertion | |
# Matches if the searched substring is not preceded by a match for '...'. | |
# Note that the contained pattern must only match strings of some fixed length. | |
print re.findall(r"^(?<!:tag)(\b\w*\b)", string) | |
# ['begin'] | |
'(?(id/name)yes-pattern|no-pattern)' | |
# Will try to match with yes-pattern if the group with given id or name exists, | |
# and with (optional) no-pattern if it does not. | |
print_match(re.match("(<)?[^<]*(?(1)>)", "<identifier>text")) | |
# <identifier> | |
print_match(re.match("(<)?[^<]*(?(1)>)", "text<identifier>")) | |
# text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment