0

I have a list of strings like:

1,-102a
1,123-f
1943dsa
-da238,
-,dwjqi92

How can I make a Regex expression in Python that matches as long as the string contains the characters , AND - regardless of the order or the pattern in which they appear?

Brian
  • 13
  • 4

1 Answers1

1

I would use the following regex alternation:

,.*-|-.*,

Sample script:

inp = ['1,-102a', '1,123-f', '1943dsa', '-da238,', '-,dwjqi92']
output = [x for x in inp if re.search(r',.*-|-.*,', x)]
print(output)

This prints:

['1,-102a', '1,123-f', '-da238,', '-,dwjqi92']
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360