简体   繁体   English

正则表达式匹配包含至少3个破折号/连字符的字符串

[英]Regex to match a string that contains at least 3 dashes / hyphens

I have an email body. 我有一个电子邮件正文。 It contains several lines of text. 它包含几行文字。 I need to extract the first occurrence of a string that: 我需要提取第一次出现的字符串:

  1. comes after a specific text 在特定文本之后
  2. contains at least 3 dashes 包含至少3个破折号

The shape of the dashed string is unknown. 虚线的形状未知。 It may contain letters and numbers of any number, ie: AA3A-123-NNN-D or 12-OOO-12455-AS 它可能包含任何数字的字母和数字,即:AA3A-123-NNN-D或12-OOO-12455-AS

For example: 例如:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor坐下来,精致的adipistur elit。 Donec imperdiet porta libero ac imperdiet. Donec imperdiet porta libero ac imperdiet。

Nam enim nisl: aliquam ut feugiat vitae Nam enim nisl:aliquam ut feugiat vitae

Specific text after which I need to search: Etiam rhoncus AAFA-12X-DDDD-12 metus risus More text: foo 我需要搜索的具体文字: Etiam rhoncus AAFA-12X-DDDD-12 metus risus更多文字:foo

Target language is C#. 目标语言是C#。

I've tried doing something like ([A-Za-z0-9]{5}-[A-Za-z0-9]{4}-[A-Za-z0-9]{3}-[A-Za-z0-9]{5}) but as you can see here I need to set the shape of the string which is not always known. 我尝试过像([A-Za-z0-9]{5}-[A-Za-z0-9]{4}-[A-Za-z0-9]{3}-[A-Za-z0-9]{5})但正如你在这里看到的,我需要设置不总是已知的字符串的形状。

You can use a lazy quantifier with [\\s\\S] : 你可以使用[\\s\\S]的延迟量词:

(?:Specific\ text\ after\ which\ I\ need\ to\ search:)
[\s\S]+?\K
(\b\w+-\w+-\w+-\w+\b)

The \\b is a word boundary, \\K deletes everything to the left from the match. \\b是单词边界, \\K删除匹配项左侧的所有内容。
See a demo on regex101.com . 请参阅regex101.com上的演示

If your expression contains an unknown number of letters and numbers the best you can do is specify a range for your regular expression. 如果您的表达式包含未知数量的字母和数字,您可以做的最好的事情是指定正则表达式的范围。 I see in your examples the most a block has is 5 characters and least one has is 1 character. 我在你的例子中看到,一个块最多有5个字符,最少一个有1个字符。

So something like this will capture it, 所以像这样的东西会抓住它,

([A-Za-z0-9]{1,5}-[A-Za-z0-9]{1,5}-[A-Za-z0-9]{1,5}-[A-Za-z0-9]{1,5})

I'd go with something like (?:[a-zA-Z0-9]+-){3,}[a-zA-Z0-9]+ . 我会选择(?:[a-zA-Z0-9]+-){3,}[a-zA-Z0-9]+ What this will do is match 3 or more groups of alphanumerics ending with a dash, followed by one that doesn't. 这将做的是匹配3个或更多以字母结尾的字母数字组,然后是不支持的字母组。

Try it yourself on Regex101 . 在Regex101上自己试试吧

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

相关问题 使用正则表达式匹配包含数字字母和短划线的字符串 - Using Regular Expression Match a String that contains numbers letters and dashes 正则表达式匹配包含括号的字符串 - regex to match string that contains brackets 仅当包含至少1个指定字符时,正则表达式才匹配任何2个字符组合 - Regex match any 2 character combinations only if contains at least 1 specified character 删除破折号但不是连字符 - Remove Dashes but Not Hyphens 正则表达式:字符串至少包含一组大括号 - Regex: string contains at least one set of curly brackets JsonConverter 不会用破折号(连字符)转换枚举 - JsonConverter will not convert enum with dashes(hyphens) 如何验证字段仅包含字母数字、空格、破折号并始终包含至少一个字母或数字字符? - How to validate a field only contains alphanumerics, spaces, dashes & always contains at least one alpha or numeric character? 正则表达式 - 仅在字符串包含任何字母字符时匹配字符串 - Regex - Match a string only when it contains any alphabetic characters 正则表达式匹配包含特定格式的所有单词的字符串 - Regex To Match String With All Words Contains Certain Format 如果字符串的长度为2并且包含1个字母和1个数字,则正则表达式匹配 - Regex match if a string has length 2 and contains 1 letter and 1 number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM