abcbucket
abcbucket

Reputation: 29

Regex in .net finding all occurances after a certain string

Very new to regexp. I want to match all occurrences of a string which occurs after a certain pattern but I am not able to come up with a regex to do that (in .net).

The string looks something like this:

MAIN SKUID="AAAAAA" any number of characters here  AdHocID="123456" some characters AdHocID="856322224" ..some chars again etc.....

I want to find all ooccurances of "AdHocID=digits (including the quotation marks) " only if there is a

MAIN SKUID="AAAAAA" somewhere before. If SKUID is not AAAAAA then I don't need those AdhocID strings.

Currently I am using regex pattern

(?<= SKUID="AAAAAA" )(?:.*)AdHocID={d,} .*

I searched through the threads and tried some patterns with no luck

Upvotes: 0

Views: 38

Answers (2)

Sebastian Proske
Sebastian Proske

Reputation: 8413

Instead of the lookbehind you can also make use of continuous matching by using the \G token, like

(?:\G(?!^)|SKUID="AAAAAA").*?\b(AdHocID="\d+")

Demo

Upvotes: 0

The fourth bird
The fourth bird

Reputation: 163277

You could add the .* part to the lookbehind which is supported in .NET.

To match the digits you should use \d+. Using curly braces that would look like \d{1,}

(?<=SKUID="AAAAAA" .*)\bAdHocID="\d+"

.NET regex demo

Upvotes: 1

Related Questions