0

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
3
  • 1
    commenting out the line that sets charstr to '''^ S[SPQWR]{1}R''' makes your regex work. Is there any significance to that part? Commented Nov 9, 2017 at 17:15
  • 1
    You have a space after the caret, plus you're including the single quotes in the string. Why? The regex string you're after is ^S[SPQWR]R, so why not just '^{}'.format(charstr)? Commented Nov 9, 2017 at 17:20
  • I thought you had to specify a regular expression in that way. Thanks. I have commented out that line in my code to reflect your suggestion. Commented Nov 9, 2017 at 17:22

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.