Pure Danger Tech


navigation
home

Multiple regex matches

13 Oct 2006

I know I have written this code multiple times from scratch so I will blog it here for perpetuity (and some smarter people than me can suggest better solutions).

The problem is that I want to write a regex with a capturing group, then run it on a bunch of text and get back all the captured groups from all the matches. The best way I can figure out how to do this is:

`I know I have written this code multiple times from scratch so I will blog it here for perpetuity (and some smarter people than me can suggest better solutions).

The problem is that I want to write a regex with a capturing group, then run it on a bunch of text and get back all the captured groups from all the matches. The best way I can figure out how to do this is:

`

This code is assuming that the pattern has exactly one capture group. It could be extended to handle multiple capture groups pretty easily.

So an example might be that I have some text (say a file of phone numbers) and I want to match each phone number and return all the area codes (just the area codes). So, the text might be:

``I know I have written this code multiple times from scratch so I will blog it here for perpetuity (and some smarter people than me can suggest better solutions).

The problem is that I want to write a regex with a capturing group, then run it on a bunch of text and get back all the captured groups from all the matches. The best way I can figure out how to do this is:

`I know I have written this code multiple times from scratch so I will blog it here for perpetuity (and some smarter people than me can suggest better solutions).

The problem is that I want to write a regex with a capturing group, then run it on a bunch of text and get back all the captured groups from all the matches. The best way I can figure out how to do this is:

`

This code is assuming that the pattern has exactly one capture group. It could be extended to handle multiple capture groups pretty easily.

So an example might be that I have some text (say a file of phone numbers) and I want to match each phone number and return all the area codes (just the area codes). So, the text might be:

``

And the answer should be 111, 222, and 333. So, you’d do something like this with my method:

```I know I have written this code multiple times from scratch so I will blog it here for perpetuity (and some smarter people than me can suggest better solutions).

The problem is that I want to write a regex with a capturing group, then run it on a bunch of text and get back all the captured groups from all the matches. The best way I can figure out how to do this is:

`I know I have written this code multiple times from scratch so I will blog it here for perpetuity (and some smarter people than me can suggest better solutions).

The problem is that I want to write a regex with a capturing group, then run it on a bunch of text and get back all the captured groups from all the matches. The best way I can figure out how to do this is:

`

This code is assuming that the pattern has exactly one capture group. It could be extended to handle multiple capture groups pretty easily.

So an example might be that I have some text (say a file of phone numbers) and I want to match each phone number and return all the area codes (just the area codes). So, the text might be:

``I know I have written this code multiple times from scratch so I will blog it here for perpetuity (and some smarter people than me can suggest better solutions).

The problem is that I want to write a regex with a capturing group, then run it on a bunch of text and get back all the captured groups from all the matches. The best way I can figure out how to do this is:

`I know I have written this code multiple times from scratch so I will blog it here for perpetuity (and some smarter people than me can suggest better solutions).

The problem is that I want to write a regex with a capturing group, then run it on a bunch of text and get back all the captured groups from all the matches. The best way I can figure out how to do this is:

`

This code is assuming that the pattern has exactly one capture group. It could be extended to handle multiple capture groups pretty easily.

So an example might be that I have some text (say a file of phone numbers) and I want to match each phone number and return all the area codes (just the area codes). So, the text might be:

``

And the answer should be 111, 222, and 333. So, you’d do something like this with my method:

```

This works great for me. Is there a better way to do this? Is there some magical option on the regex pattern itself to avoid doing the loop?