P
proctor
hello,
i hope this is the correct place...
i have an issue with some regex code i wonder if you have any insight:
================
import re, sys
def makeRE(w):
print w + " length = " + str(len(w))
reString = "r'" + w[:1]
w = w[1:]
if len(w) > 0:
for c in (w):
reString += "|" + c
reString += "'"
print "reString = " + reString
return reString
test = sys.argv[1]
stg = sys.argv[2]
while test:
print "test = ", test
print "stg = ", stg
rx_a = re.compile(makeRE(test))
i = rx_a.search(stg).start()
print "i = " + str(i)
id = test.find(stg)
test = test[:id] + test[id+1:]
print "test == ", test
stg = stg[:i] + stg[i+1:]
print
================
i get the following output:
================
test = abc
stg = defabc
abc length = 3
reString = r'a|b|c'
i = 4
test == ac
test = ac
stg = defac
ac length = 2
reString = r'a|c'
Traceback (most recent call last):
File "aaaa.py", line 21, in ?
i = rx_a.search(stg).start()
AttributeError: 'NoneType' object has no attribute 'start'
=================
i am fairly new to this, and can't see the reason for the error. what
am i missing?
btw, i think there are simpler ways to go about this, but i am doing it
this way (regexs) for a bit of a challenge and learning experience.
thanks to all!
sincerely,
proctor
i hope this is the correct place...
i have an issue with some regex code i wonder if you have any insight:
================
import re, sys
def makeRE(w):
print w + " length = " + str(len(w))
reString = "r'" + w[:1]
w = w[1:]
if len(w) > 0:
for c in (w):
reString += "|" + c
reString += "'"
print "reString = " + reString
return reString
test = sys.argv[1]
stg = sys.argv[2]
while test:
print "test = ", test
print "stg = ", stg
rx_a = re.compile(makeRE(test))
i = rx_a.search(stg).start()
print "i = " + str(i)
id = test.find(stg)
test = test[:id] + test[id+1:]
print "test == ", test
stg = stg[:i] + stg[i+1:]
================
i get the following output:
================
test = abc
stg = defabc
abc length = 3
reString = r'a|b|c'
i = 4
test == ac
test = ac
stg = defac
ac length = 2
reString = r'a|c'
Traceback (most recent call last):
File "aaaa.py", line 21, in ?
i = rx_a.search(stg).start()
AttributeError: 'NoneType' object has no attribute 'start'
=================
i am fairly new to this, and can't see the reason for the error. what
am i missing?
btw, i think there are simpler ways to go about this, but i am doing it
this way (regexs) for a bit of a challenge and learning experience.
thanks to all!
sincerely,
proctor