using a string instead of a char to deliminate

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
phazei
Forum Newbie
Posts: 1
Joined: Mon Aug 20, 2007 9:49 pm

using a string instead of a char to deliminate

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

Why not use a lookahead. This regex matches everything until 'hi' is reached.

Code: Select all

/.*(?=hi)/s
Post Reply