Home > Net >  Search for variations of a regex pattern
Search for variations of a regex pattern

Time:01-30

I have a text like inuput_text = "this is an example test". normally if I want to find "example test" in the input_test I do it like this:

if "example test" in inuput_text:
    print("we have a match")

Now what if I have other variety of "example test" like "example_test" or "example-test" and I want to match them with input_text?

I think that regex would be okay but I'm confused here.

Edit:

I think of regex to check if we have a white space and then genrate a list of strings replacing the white space with _ or -, and after that I can search for these generated strings in the input text.

CodePudding user response:

You can transform your search term to a regex, then match that.

import re

pattern = re.escape(search_term).replace(r"\ ", "[ _-]")
match = re.search(pattern, input_text)
if match:
   ...

CodePudding user response:

Check the below code:

import re

input_text = "this is an example_test"
match = re.search("example[-_ ]test", input_text)

print(match.group()) 

You can use the regex "example[-_ ]test" to search for the text and you can use match.group() to get the matched string. You can define the characters between the example and test inside [].

To know more, follow this link: Python RegEx Match Object

CodePudding user response:

If you can list out all the possible variations then you can search for all of them by creating a regex that does that. Here's what I mean:

import re


possibilities = "example test", "example_test", "example-test"
escaped = {re.escape(possibility) for possibility in possibilities}
pattern = '|'.join(escaped)
regex = re.compile(pattern)

test_strings = """\
Lorem ipsum dolor example test sit amet
Quisque semper, tellus quis pharetra viverra.
Fusce example_test molestie nisi enim
Curabitur convallis viverra example-test dolor vel tristique.
Ut nunc ante, fermentum example*test.
""".splitlines()

for input_text in test_strings:
    print(f'{input_text=!r}')
    match = regex.search(input_text)
    if match:
        print(f'    Matches: {match.group()!r}')
    else:
        print("    Doesn't match anything.")

Here's the results:

input_text='Lorem ipsum dolor example test sit amet'
    Matches: 'example test'
input_text='Quisque semper, tellus quis pharetra viverra.'
    Doesn't match anything.
input_text='Fusce example_test molestie nisi enim'
    Matches: 'example_test'
input_text='Curabitur convallis viverra example-test dolor vel tristique.'
    Matches: 'example-test'
input_text='Ut nunc ante, fermentum example*test.'
    Doesn't match anything.
  •  Tags:  
  • Related