Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My regex works on regex101 but doesn't work in python? [duplicate]

Tags:

python

regex

So I need to match strings that are surrounded by |. So, the pattern should simply be r"\|([^\|]*)\|", right? And yet:

>>> pattern = r"\|([^\|]*)\|"
>>> re.match(pattern, "|test|")
<_sre.SRE_Match object at 0x10341dd50>
>>> re.match(pattern, "  |test|")
>>> re.match(pattern, "asdf|test|")
>>> re.match(pattern, "asdf|test|1234")
>>> re.match(pattern, "|test|1234")
<_sre.SRE_Match object at 0x10341df30>

It's only matching on strings that begin with |? It works just fine on regex101 and this is python 2.7 if it matters. I'm probably just doing something dumb here so any help would be appreciated. Thanks!

like image 737
mdong Avatar asked Dec 15 '16 02:12

mdong


People also ask

How do you do multiple regex in Python?

made this to find all with multiple #regular #expressions. regex1 = r"your regex here" regex2 = r"your regex here" regex3 = r"your regex here" regexList = [regex1, regex1, regex3] for x in regexList: if re. findall(x, your string): some_list = re. findall(x, your string) for y in some_list: found_regex_list.

Can you use regex in replace Python?

In this tutorial, you will learn about how to use regex (or regular expression) to do a search and replace operations on strings in Python. Regex can be used to perform various tasks in Python. It is used to do a search and replace operations, replace patterns in text, check if a string contains the specific pattern.


2 Answers

re.match will want to match the string starting at the beginning. In your case, you just need the matching element, correct? In that case you can use something like re.search or re.findall, which will find that match anywhere in the string:

>>> re.search(pattern, "  |test|").group(0)
'|test|'

>>> re.findall(pattern, "  |test|")
['test']
like image 143
David542 Avatar answered Sep 18 '22 15:09

David542


In order to reproduce code that runs on https://regex101.com/, you have to click on Code Generator on the left handside. This will show you what their website is using. From there you can play around with flags, or with the function you need from re.

Note:

  • https://regex101.com/ uses re.MULTILINE as default flag
  • https://regex101.com/ uses re.finditer as default method
import re

regex = r"where"

test_str = "select * from table where t=3;"

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
like image 21
louis_guitton Avatar answered Sep 18 '22 15:09

louis_guitton