views:

34

answers:

2

i need to match a pattern using reMatchNoCase("(listid)","listid car van listid dog cat listid house hotel")> so listid is the pattern and match that and everything to the next pattern witch is listid again. so if i dump the rematch ill get a structure each starting with listid and the content within

this is what it should look like

  1. listid car van

  2. listid dog cat

  3. listid house hotel

etc....

when i use reMatchNoCase("(listid)","listid car van listid dog cat listid house hotel")> it will only create a structure with "listid" and nothing more like car van... what regex do i use after (listid) to get everything within?

thanks

+2  A: 

You can use this regex

listid.*?(?=(listid|$))

What this regex does is locate a 'listid' followed by any characters until next 'listid' or 'end of line ($)'. The ?= is the positive lookahead to look for the next 'listid' but not make it part of the match. The ? in .*? avoids greedy behavior of .* and matches immediate next lookahead string.

Gopi
a life saver isn't the word!!! thanks
loo
What do I need the $ newline for? Will it mess-up by a actual new line if I don't want it to??
loo
Since there is no 'listid' at the end, we have to have something to look ahead for capturing the last match. Hence the use of '$'. If you remove it it will not match the 'listid house hotel' in your example.
Gopi
And I dont know how it is exactly done in coldfusion, but generally languages provide a way to support regex in 'multiline' mode. You may try to find that out. Also regex has a modifier '/m' you can try to use if at all you face any problem
Gopi
CF uses Apache ORO regex engine, which supports the `(?m)` inline modifier. Alternatively, just use `\z` instead of `$` to avoid worrying about newlines and only match at end of string.
Peter Boughton
Oh, and there's no need for the parentheses inside the lookahead.
Peter Boughton
+2  A: 

This regular expression will probably do; I don't know coldfusion details, but it matches what you want:

listid.*?(?=$|\slistid)
Artefacto
a life saver isn't the word!!! thanks
loo