-1
filename = Baked Beans And Spam.txt

I can understand how regex uses strings:

f = self.filename
v = self.AdvanceReplace.GetValue()  # from a TextCtrl box
s = re.sub(r'\sAnd\s',' & ',f)    
prints Baked Beans & Spam

But suppose I want to use the first argument as a variable such as v:

s = re.sub(v,' & ',f)

prints Baked Beans And spam. How can I force re.sub to accept this as a variable?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
George
  • 269
  • 2
  • 5
  • 11
  • What do you mean? Is `v` not a string? – arshajii Aug 20 '13 at 22:12
  • You probably want `s = re.sub(r'\sAnd\s',v,f)`? – alecxe Aug 20 '13 at 22:14
  • no v is what I'll be entering in my TextCtrl box. I'm using regular expressions for v – George Aug 20 '13 at 22:25
  • If v is a string it should work fine, except that it won't be raw. In your test code, what do you get when you print `v`. Also there are presumably quotes around your `filename` definition? – beroe Aug 20 '13 at 23:15
  • when I use the expression r'\sAnd\s',v,f I get the same back. v = r'\sAnd\s',v,f – George Aug 20 '13 at 23:44
  • I still can't figure how you can use the regex as a varible instead of a string – George Aug 20 '13 at 23:51
  • Which language treats `filename` as a string with no quotes around the assigned value? Or, which language are you working in? Some people recognize it (it might be malformed Python to my eyes), but it would make sense to identify the host language. – Jonathan Leffler Aug 23 '13 at 03:01

1 Answers1

0

I learned regex expressions are just charactors. If you omit the quotes, the TextCtrl will carry on the expression. Example: \sAnd\s not r'\sAnd\s'

George
  • 269
  • 2
  • 5
  • 11