python - Find several strings with regular expressions -
i'm looking or capability match on several strings regular expressions.
# find either "-hex", "-mos", or "-sig" # result -hex, -mos, or -sig # see want rid of double quotes around these 3 strings. # other double quoting ok. # i'd like. messwithcommandargs = ' -o {} "-sig" "-r" "-sip" ' messwithcommandargs = re.sub( r'"(-[hex|mos|sig])"', r"\1", messwithcommandargs) this works:
messwithcommandargs = re.sub( r'"(-sig)"', r"\1", messwithcommandargs)
square brackets character classes can match single character. if want match multiple character alternatives need use group (parentheses instead of square brackets). try changing regex following:
r'"(-(?:hex|mos|sig))"' note used non-capturing group (?:...) because don't need capture group, r'"(-(hex|mos|sig))"' work same way since \1 still quotes.
alternative use r'"-(hex|mos|sig)"' , use r"-\1" replacement (since - no longer part of group.
Comments
Post a Comment