Regular expressions are superb for text processing, but I have found
that Non-Greedy matches do not get enough press. So I'm going to do my
bit to promote them.
If you're using regular expressions, but only ever use ? * and + you are seriously Missing a Trick!
The non-greedy operators are essential for efficient data extraction from formats such as HTML.
If you're using regular expressions, but only ever use ? * and + you are seriously Missing a Trick!
| Greedy Operators - will consume as much as possible | ||
| ? | 0 or 1 | One if available |
| * | 0 or more | As many as will match |
| + | 1 or more | As many as will match |
| Non-Greedy Operators - take as few a possible | ||
| ?? | 0 or 1 | Prefers zero to one |
| *? | 0 or more | Prefers zero or as few as possible |
| +? | 1 or more | Will take one, but avoid others if possible |
The non-greedy operators are essential for efficient data extraction from formats such as HTML.
332 comments,
Web, Tuesday, May 10, 2005 09:42


