简体   繁体   English

使用 REGEX 忽略特定单词后需要文本

[英]Require text after ignoring a specific words using REGEX

I would like to extract words after ignoring certain key words.我想在忽略某些关键字后提取单词。

I want to ignore supplied by or supplied by M/S until the end of line.我想忽略由 M/S提供由 M/S 提供,直到行尾。

Input Text输入文本

Remittance Advice汇款通知

This section includes details as supplied by M/S MOBIS INDIA LIMITED本部分包括由 M/S MOBIS INDIA LIMITED 提供的详细信息

This section includes details as supplied by MANDO AUTOMOTIVE INDIA LIMITED本部分包括由 MANDO AUTOMOTIVE INDIA LIMITED 提供的详细信息

This section includes details as supplied by MAHINDRA AND MAHINDRA AUTO SECTOR本部分包括由 MAHINDRA 和 MAHINDRA AUTO SECTOR 提供的详细信息

Expected Output预期产出

MOBIS INDIA LIMITED摩比斯印度有限公司

MANDO AUTOMOTIVE INDIA LIMITED万都汽车印度有限公司

MAHINDRA AND MAHINDRA AUTO SECTOR MAHINDRA 和 MAHINDRA 汽车行业

I have given something like this我给了这样的东西

(?<=(supplied by.)|(supplied by M/S.)).* (?<=(由.)|(由 M/S.提供.)).*

Appreciate help here.感谢这里的帮助。

If you want a match only, you can use lookarounds, and optionally match M/S in the lookbehind and asserting that when starting the match it is not going to be M/S如果您只想要匹配,则可以使用环视,并可选择在后视中匹配M/S并断言在开始匹配时不会是M/S

(?<=supplied by\s+(?:M/S\s+)?)\S(?<!\s+M(?=/S\s)).+

The pattern matches:模式匹配:

  • (?<= Positive lookbehind (?<=正向后视
    • supplied by\\s+ Match literally followed by 1+ whitespace chars supplied by\\s+匹配字面后跟 1+ 个空格字符
    • (?:M/S\\s+)? Optionally match M/S and 1+ whitespace chars可选匹配M/S和 1+ 空白字符
  • ) Close lookbehind )关闭回溯
  • \\S Match a non whitespace character \\S匹配一个非空白字符
  • (?<! Negative lookbehind (?<!负向后视
    • \\s+M(?=/S\\s) Match 1+ whitespace chars, M and assert S followed by a whitespace char \\s+M(?=/S\\s)匹配 1+ 个空白字符, M并断言S后跟一个空白字符
  • ) Close lookbehind )关闭后视
  • .+ Match 1+ times any character .+匹配任意字符 1+ 次

.NET regex demo .NET 正则表达式演示

Another way to write it could be matching 1+ non whitespace chars followed by asserting a whitespace boundary to the right, and then assert that directly to the left is not M/S另一种编写它的方法可能是匹配 1+ 个非空白字符,然后在右侧断言空白边界,然后断言直接在左侧不是M/S

(?<=supplied by\s+(?:M/S\s+)?)\S+(?!\S)(?<!M/S).+

See another .NET regex demo查看另一个.NET 正则表达式演示

Another option is to use a capture group instead:另一种选择是改用捕获组:

\bsupplied by(?:\s+M\/S)?\s+(.+)

Regex demo正则表达式演示

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

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