简体   繁体   English

c#正则表达式只在每个句子中第一个匹配

[英]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) 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, Response.Write(Dictionary.gsTranslate(oClientinfo,"Es konnten keine Befehle in der Auswahl dargestellt werden,
" & _ "das Design kann trotzdem modifiziert werden.")) " & _ "das Design kann trotzdem modifiziert werden。"))

Regex:正则表达式:

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

Example & output: https://regexr.com/4rpfj示例和输出: 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) "BatchDesc", "tdDebitAdviceBatches", "BatchID=" & lBatchID)

Response.Write(Dictionary.gsTranslate(oClientinfo,"Es konnten keine Befehle in der Auswahl dargestellt werden, Response.Write(Dictionary.gsTranslate(oClientinfo,"Es konnten keine Befehle in der Auswahl dargestellt werden,
" & _ "das Design kann trotzdem modifiziert 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 .所以我不想要文本中的第一个匹配项,我想要每个Dictionary.gsTranslate之后的第一个匹配项。

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)dictionary\\.gstranslate\\([^)]+, *)正向后视确保以下前置
    • (?i) enables case-insensitive flag (?i)启用不区分大小写的标志
    • dictionary\\.gstranslate matches dictionary.gstranslate literally (case-insensitive) dictionary\\.gstranslate从字面上匹配dictionary.gstranslate (不区分大小写)
    • \\( 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 " "[^)]+"匹配" ,然后匹配除)以外的任何字符一次或多次,然后"

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:从我所见, \\n|\\r 允许匹配包含并通过“BatchDesc”......由于换行符应该与下划线字符一起出现(从我在你的文本中看到的)也许这会满足您的需求:

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

I just added the mandatory underscore before the \\n and \\r.我只是在 \\n 和 \\r 之前添加了强制下划线。

https://regexr.com/4rrv0 https://regexr.com/4rrv0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM