1

I would like to match a string with something like:

re.match(r'<some_match_symbols><my_match><some_other_match_symbols>', mystring)  

where mymatch is a string I would like it to find. The problem is that it may be different from time to time, and it is stored in a variable. Would it be possible to add one variable to a regex?

2
  • 2
    Yes, before you compile it the regexp is just a regular string. so re.compile(constant_stuff+varying_stuff+more_constant_stuff) works fine. Commented Jan 3, 2013 at 0:48
  • Sounds like you are looking for string concatenation: docs.python.org/2/tutorial/introduction.html#strings. Commented Jan 3, 2013 at 0:48

3 Answers 3

2

Nothing prevents you from simply doing this:

re.match('<some_match_symbols>' + '<my_match>' + '<some_other_match_symbols>', mystring)

Regular expressions are nothing else than a string containing some special characters, specific to the regular expression syntax. But they are still strings, so you can do whatever you are used to do with strings.

The r'…' syntax is btw. a raw string syntax which basically just prevents any escape sequences inside the string from being evaluated. So r'\n' will be the same as '\\n', a string containing a backslash and an n; while '\n' contain a line break.

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

Comments

0
import re
url = "www.dupe.com"
expression = re.compile('<p>%s</p>'%url)
result = expression.match("<p>www.dupe.com</p>BBB")
if result:
  print result.start(), result.end()

2 Comments

Nothing wrong with this, but just a small note on the variable naming (I suspect “mather” should have been “matcher”?): re.compile does not return a matcher as other languages do, but returns simply a compiled regular expression, that is completely reusable and independent from the text it operates on. You can do other things on that expression other than matching (replacing is fine too), so expression or urlExpression in this case might be more appropriate :)
Yeah, that’s what I meant to say :)
-1

The r'' notation is for constants. Use the re library to compile from variables.

7 Comments

"The r'' notation is for constants.": Uh? There ain't no constants in Python. r'...' just denotes a raw string (i.e. each character is treated literally).
@FelixKling -- are you saying I can change the value of, say, 3?
No, because 3 is a number literal. r'...' is a raw string literal. Literals denote values that don't have to be computed during runtime. They cannot be on the RHS of an assignment, so you cannot change them. Neither of them is a constant though. No one prevents you from assigning the string to a variable and passing that variable to re.match.
@FelixKling -- is a "literal" not constant? Can I change the value of constants? Of literals?
'foo' is just as constant as r'bar' or 42. That doesn’t change the fact that the first two are both string literals, and as such strings, while the latter is a number literal, in this case an int. The constant-ness of things has nothing to do with this question.
|

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.