FROM : Kevin Ballard
DATE : Sat Nov 06 22:54:42 2004
Uhh, that first match won't even work - your regex requires a ( at the
beginning of the string.
I do see what you mean how if you use an alternation you could possibly
get a URL surrounded by ()'s, but then what about <> and []? And then
what about (http://www.foo.com/bar(blah).html)? Humans can tell the
inner parens are for the URL but I can't imagine how a regex can.
On Nov 6, 2004, at 12:22 PM, b.bum wrote:
> A single regex can handle that situation, it is just a pain to write.
> Using Python's regular expressions as an example (because named
> subexpressions are a lot nicer than indice based subexpressions):
>
> >>> import re
> >>> r = re.compile('^\((?P<u1>http://[^)]*)|(?P<u2>http://.*)')
> >>> r.match('(http://foo.com/baz)bar').group('u1')
> 'http://foo.com/baz'
> >>> r.match('http://foo.com/baz/bar').group('u2')
> 'http://foo.com/baz/bar'
>
> The '|' -- or operator -- is the key. Ordering the expressions is
> equally as important as you must have the most specific matching
> expression first.
--
Kevin Ballard
<email_removed>
http://www.tildesoft.com
http://kevin.sb.org
DATE : Sat Nov 06 22:54:42 2004
Uhh, that first match won't even work - your regex requires a ( at the
beginning of the string.
I do see what you mean how if you use an alternation you could possibly
get a URL surrounded by ()'s, but then what about <> and []? And then
what about (http://www.foo.com/bar(blah).html)? Humans can tell the
inner parens are for the URL but I can't imagine how a regex can.
On Nov 6, 2004, at 12:22 PM, b.bum wrote:
> A single regex can handle that situation, it is just a pain to write.
> Using Python's regular expressions as an example (because named
> subexpressions are a lot nicer than indice based subexpressions):
>
> >>> import re
> >>> r = re.compile('^\((?P<u1>http://[^)]*)|(?P<u2>http://.*)')
> >>> r.match('(http://foo.com/baz)bar').group('u1')
> 'http://foo.com/baz'
> >>> r.match('http://foo.com/baz/bar').group('u2')
> 'http://foo.com/baz/bar'
>
> The '|' -- or operator -- is the key. Ordering the expressions is
> equally as important as you must have the most specific matching
> expression first.
--
Kevin Ballard
<email_removed>
http://www.tildesoft.com
http://kevin.sb.org
| Related mails | Author | Date |
|---|---|---|
| Mike O'Connor | Nov 6, 04:08 | |
| Joseph Heck | Nov 6, 04:13 | |
| Kevin Ballard | Nov 6, 04:23 | |
| John Siracusa | Nov 6, 05:05 | |
| Mike O'Connor | Nov 6, 06:12 | |
| Kevin Ballard | Nov 6, 08:59 | |
| John Stiles | Nov 6, 17:05 | |
| b.bum | Nov 6, 18:22 | |
| Kevin Ballard | Nov 6, 22:54 | |
| b.bum | Nov 7, 02:37 | |
| Kevin Ballard | Nov 7, 02:42 | |
| b.bum | Nov 7, 03:06 | |
| Kevin Ballard | Nov 7, 04:01 | |
| b.bum | Nov 7, 04:36 | |
| Eric Ocean | Nov 7, 04:44 | |
| Bertrand Mansion | Nov 7, 09:04 | |
| John Siracusa | Nov 7, 14:47 |






Cocoa mail archive

