Re: Variable in Regex

From: Christian von Essen (christian_at_mvonessen.de)
Date: 03/27/04


Date: Sat, 27 Mar 2004 22:17:34 +0100

J Hamilton wrote:
> In Perl in can put a scalar variable in a regex as the pattern.
>
> for example: m/$var/ where $var contains the string I want to match
>
> Is there a way to do this in Python?

For example:
re.search("%s" % var, StrinToMatch)
or even simpler:
re.search(var, StringToMatch)



Relevant Pages

  • Re: Variable in Regex
    ... > J Hamilton wrote: ... >> In Perl in can put a scalar variable in a regex as the pattern. ...
    (comp.lang.python)
  • Re: Is Perl *that* good?
    ... How does pattern (in the python example) come to have a 'match' ... Regex is much more 'immediate' in Perl. ...
    (comp.lang.python)
  • Regex problem from book, please help with solution
    ... I'm reading Sams Teach Yourself Perl in 24 Hours, ... chapter about regex, there is an activity: ... "See whether you can produce a pattern to match a standard time format. ...
    (comp.lang.perl.misc)
  • FAQ 6.23 How do I match a regular expression thats in a variable?
    ... How do I match a regular expression that's in a variable? ... We don't have to hard-code patterns into the match operator (or anything ... have the pattern in $regex, you use that variable in the match operator. ... and the pattern still has to be valid or Perl will complain. ...
    (comp.lang.perl.misc)
  • Re: string.remove bringt nicht das erwartete
    ... Ein Regex ist da etwas flexibler. ... pattern, "", System.Text.RegularExpressions.RegexOptions.IgnoreCase) ... Wenn man nun aber nach einem Backslash sucht ... gleichen gilt für alle anderen Metazeichen. ...
    (microsoft.public.de.german.entwickler.dotnet.vb)