Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding compiled regex in .net

Tags:

.net

regex

I have a regex that will be used repetitively where the stringLiteral will vary from one invocation to the next.

One being:

.*(^stringLiteral Number 1\r?\n)([\w|\s][^\r\n]+)(.+)

and the next being:

.*(^stringLiteral Number 2\r?\n)([\w|\s][^\r\n]+)(.+)

Is there a chance for optimization here?

EDIT: To be a bit more explicit about the live data I'm working against - I'm parsing the body an email that contains name/value pairs. I know the names (labels) and i know that the value i'm after is the line that follows the label. But I can't be sure that the name/value pairs (lines) will always fall in the same order - so I can't build one large expression.

I have to build multiple expressions the discard everything from the beginning of the block to and including the given label (this would be the stringLiteral); capture the next line into a capture group; then discard everything following that line.

so this line capture the Name field

myOrder.Name = Regex.Replace(resultString, @".*(^Name\r\n)([\w|\s][^\r\n]+)(.+)", "$2", RegexOptions.Multiline | RegexOptions.Singleline);

and this line captures the price field

myOrder.Price= Regex.Replace(resultString, @".*(^Price\r\n)([\w|\s][^\r\n]+)(.+)", "$2", RegexOptions.Multiline | RegexOptions.Singleline);
like image 448
justSteve Avatar asked Jan 29 '26 00:01

justSteve


1 Answers

Well, you could condense them into a single expression if you want to:

.(^stringLiteral Number [12]\r?\n)([\w|\s][^\r\n]+)(.+)

If you post an example of the input you want to match or capture I could probably help some more.

like image 64
Andrew Hare Avatar answered Jan 30 '26 13:01

Andrew Hare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!