DeeeeRoy
DeeeeRoy

Reputation: 487

Python: String to Regex Pattern

Suppose I have a string of characters.

charstr = "SZR"

Suppose that the character Z is a loaded character and can represent S, P, Q, W, or R

I want to write a function get_regex(charstr) which takes charstr as an input and returns a regular expression string. Which can then be used to find patterns in other strings.

The following code should not results in answer = 'None', since SZR matches SRR and SWR, which are in SQSRRSWR.

charstr = "SZR"
answer = re.search(get_regex(charstr), 'SQSRRSWR')

I have tried the following but it won't work. Any suggestions?

import re
def get_regex(charstr):
    charstr = re.sub("Z", "[SPQWR]{1}", charstr) # Z: can be S,P,Q,W, or R
    #The line directly below this was in my original post.  I have commmented it out and the function now works properly.
    #charstr = "\'\'\'^ " + charstr + "\'\'\'"    # Results in '''^ S[SPQWR]{1}R'''
    return charstr

charstr = "SZR"
answer = re.search(get_regex(charstr), 'SQSRRSWR')
print(answer)                                    # Results in None

Upvotes: 0

Views: 676

Answers (1)

sniperd
sniperd

Reputation: 5274

Your example seems to be pretty close to working. If I'm understanding what you are trying to do, this works:

import re

def get_regex(charstr):
    charstr = re.sub("Z", "[SPQWR]", charstr) # Z: can be S,P,Q,W, or R
    return charstr

charstr = "SZR"
if re.search(get_regex(charstr), 'SQSRRSWR'):
    print("yep it matched")
else:
    print("nope it does not match")

charstr = "SXR"
if re.search(get_regex(charstr), 'SQSRRSWR'):
    print("yep it matched")
else:
    print("nope it does not match")

results in:

yep it matched
nope it does not match

Which looks to be what you are trying to do. I took out the {1} since that is implied. Just toss a comment in if it doesn't seem right and I'll update this answer.

Upvotes: 1

Related Questions