Page 1 of 1

using a string instead of a char to deliminate

Posted: Mon Aug 20, 2007 10:08 pm
by phazei
I've used regexp's for a while and have come to think I'm pretty good with them.
Though there was a problem I came across a while ago that I managed how to figure out another way that worked well. But I never was able to find an answer to the first way I tried to solve it. I just found this forum so I figured I'd ask here.

I wanted to be able to use this: [^_]* (where _ is any character)

with a string (where _ is a string) instead of just characters. I tried grouping and couldn't get it to work with a variety of different combinations of structures such as look ahead.

This is my goal:

[^(?:hi)]* so it continues until it reaches "hi" not just "h" or "i".

Maybe [^[[h]{1}{1}]{2}] ?

It would be nice to be able to use it for any string.
It's easy enough to search for a word, but to use a word as a deliminator I can't figure out.

Posted: Mon Aug 20, 2007 10:27 pm
by superdezign
Once you put characters into a character class, which is anything in brackets [], it is a selection of characters, not a sequence of characters. You can't nest character classes. It seems to me that you had a fundamental misunderstanding early on the learning process.

To select data up to a particular string, i.e. 'so it continues until it reaches "hi"':

Code: Select all

(.*?)hi
Fairly simple.

Posted: Tue Aug 21, 2007 2:44 am
by GeertDD
Why not use a lookahead. This regex matches everything until 'hi' is reached.

Code: Select all

/.*(?=hi)/s