regex - How to match exception with double character with Python regular expression? -
got string , regex findall:
txt = """ dx d_2,222.22 ,, dy h..{3,333.33} ,, dz b#(1,111.11) ,, dx-ay relative 4,444.44 ,, """ n in re.findall( r'([-\w]+){1}\w+([^,{2}]+)\s+,,\w+', txt ) : axis, value = n print "a:", axis print "v:", value
in second (value) group trying match except double commas, seems catch 1 ","
. can got in example simple (.*?)
reasons got except ",,"
. thank you.
edit: see want accomplish use r'([-\w]+){1}\w+(.*?)\s+,,\w+'
instead. give such output:
a: dx v: d_2,222.22 a: dy v: h..{3,333.33} a: dz v: b#(1,111.11) a: dx-ay v: relative 4,444.44
edit #2: please, answer did not include double comma exception not needed. there solution...should be. patern :
any whitespace - word possibly "-"
- " "
- , ",,"
except itself.
[^,{2}]
character class matches character except: ',', '{', '2', '}'
with "character class", called "character set", can tell regex engine match
only 1 out of several characters
.
it should ([^,]{2})+
( group , capture \1 [^,]{2} character except: ',' (2 times) )+ end of \1
get matched group index 1 , 2
([-\w]+)\s+(.*?)\s+,,
here online demo
sample code:
import re p = re.compile(ur'([-\w]+)\s+(.*?)\s+,,') test_str = u"..." re.findall(p, test_str)
note: use \s*
instead of \s+
if spaces optional.
Comments
Post a Comment