Niels Lucas
Niels Lucas

Reputation: 759

c# regex only first match in each sentence

I almost had my regex but then I got a problem I cant get fixed.

Sentence:

oListView.sTranslatedPrintHeader = Dictionary.gsTranslate(oClientInfo, "Lastschriftenstapel") & " " & DataMethods.gsGetDBValue(oClientInfo, "BatchDesc", "tdDebitAdviceBatches", "BatchID=" & lBatchID)

Response.Write(Dictionary.gsTranslate(oClientinfo,"Es konnten keine Befehle in der Auswahl dargestellt werden,
" & _ "das Design kann trotzdem modifiziert werden."))

Regex:

(?<=[D-d]ictionary\.gs[T-t]ranslate.*?, ?)( ?")((.|\n|\r)*?"\))

Example & output: https://regexr.com/4rpfj

So my first match is ok, but my regex doesn't stop in that sentence and continues and then matches

"BatchDesc", "tdDebitAdviceBatches", "BatchID=" & lBatchID)

Response.Write(Dictionary.gsTranslate(oClientinfo,"Es konnten keine Befehle in der Auswahl dargestellt werden,
" & _ "das Design kann trotzdem modifiziert werden.")

What matches I want

"Lastschriftenstapel")

And

"Es konnten keine Befehle in der Auswahl dargestellt werden,<br>" & _ "das Design kann trotzdem modifiziert werden.")

Little note: there is a whole file with multiple matches. So I don't want the first match in the text, I want the first match after every Dictionary.gsTranslate.

Upvotes: 1

Views: 92

Answers (2)

Paurian
Paurian

Reputation: 1402

From what I see, the \n|\r is allowing the match to include and go past the "BatchDesc"... Since line-break continuation should occur with the underscore character (from what I see in your text) perhaps this would suit your need:

(?<=[D-d]ictionary\.gs[T-t]ranslate.*?, ?)("(.|\_ ?\n|\_ ?\r)*?"(?=\)))

I just added the mandatory underscore before the \n and \r.

https://regexr.com/4rrv0

Upvotes: 0

ctwheels
ctwheels

Reputation: 22817

You can use the following regex:

See regex in use here

(?<=(?i)dictionary\.gstranslate\([^)]+, *)"[^)]+"
# or the following with RegexOptions.IgnoreCase
(?<=dictionary\.gstranslate\([^)]+, *)"[^)]+"

How it works:

  • (?<=(?i)dictionary\.gstranslate\([^)]+, *) positive lookbehind ensuring the following precedes
    • (?i) enables case-insensitive flag
    • dictionary\.gstranslate matches dictionary.gstranslate literally (case-insensitive)
    • \( match ( literally
    • [^)]+ match any character except ) one or more times
    • , * matches comma literally, then any number of spaces
  • "[^)]+" matches ", then any character except ) one or more times, then "

Upvotes: 2

Related Questions