frakenweenie
frakenweenie

Reputation: 1

regex lookbehind followed by lookahead

INPUT: garbage="stff",start1="allshortsofCharactersExceptDoubleQuotes",start2="*&^%$blahblah"

DESIRED RESULT: allshortsofCharactersExceptDoubleQuotes

*&^%$blahblah

Using c# .NET:

string myRegExString = @"(?<=start[0-9].).*(?="")"

Yeilds: allshortsofCharactersExceptDoubleQuotes",start2="*&^%$blahblah

Through testing I know that if I replaced .* with a set that had all the characters except double quotes I would get the desired result but that is a lot of work and I will get that wrong. Also using (?!"") or (?!="") before .* does not work either.

So how do I get the lookahead to stop on the first double quote it finds?

Correct Answers (as far as I tested) from the responses:

(?<=start\d+="")[^""]*(?="")

OR

(?<=start\d+="")[^""]+(?="")

OR this works too but is not quite what was asked for.

(?<=start\d+="")[^""]*

Thanks. I was so wrapped up in the lookahead aspect of this item.

Upvotes: 0

Views: 87

Answers (3)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

You can use this:

@"(?<=start\d="")[^""]+(?="")"

the result is the whole pattern.

Upvotes: 0

rbernabe
rbernabe

Reputation: 1072

The problem with your regular expression is that .* is matching too much text. You can make your regular expression lazy by adding a question mark after the star like '.*?' Or you can change it to match every thing except double quoutes with: '[^"]*' which is what I would choose in this case. The following should work. Not tested

string myRegExString = @"(?<=start[0-9].)[^""]*(?="")"

The other solution I suggests is:

string myRegExString = @"(?<=start[0-9].).*?(?="")"

Upvotes: 0

Anirudha
Anirudha

Reputation: 32797

You should use lazy quantifier .*? which would match as less as possible..In your case .* would match as much as possible and hence it would capture till last "

(?<=start\d+="").*?(?="")

You could get a list of such of values using this code

List<string> output=Regex.Matches(input,regex)
                         .Cast<Match>()
                         .Select(x=>x.Value)
                         .ToList();

Upvotes: 1

Related Questions