0

I have a list of strings. Each string contains random text and a sequence of numbers and letters that may or may not match a regex.

Example string:

"Bla bla bla 123-abc-456 bla bla blaaha"

123-abc-456 will match a regex.

I wish to store all those matching sequences into a new list; sequence only that is, not the bla bla bla.

How could this be done? I need to break out the sequence only using the regex somehow.

6
  • 3
    Not possible. Even arbitrary string can be a regex. Commented Apr 25, 2013 at 13:08
  • bla bla bla is a valid regex Commented Apr 25, 2013 at 13:09
  • you have to chose a subset of regexs otherwise the problem is unsolvable Commented Apr 25, 2013 at 13:10
  • 1
    I think @nhahtdh, jamylak, and gipi are misreading this question. Commented Apr 25, 2013 at 13:13
  • 1
    They have a regular expression already written, and want to use it on the string. They have done no research on regular expression usage in python or the re module. They're not trying to test if there exists a regular expression inside the string which seems to be how you three are reading it. Commented Apr 25, 2013 at 13:17

2 Answers 2

1

In case you have only one "sequence" per string that you are interested in:

In [1]: import re

In [2]: re.search(r'\d{3}-\D{3}-\d{3}',
    ..: "Bla bla bla 123-abc-456 bla bla blaaha").group()
Out[2]: '123-abc-456'

Just do this in a for loop and save results to a new list.

If you want multiple matches, use re.findall as suggested above.

Sign up to request clarification or add additional context in comments.

Comments

1

Use braces in your regexp. Then, you can use groups(1), groups(2) to isolate matching parts back.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.