[英]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:
我需要提取第一次出现的字符串:
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个或更多以字母结尾的字母数字组,然后是不支持的字母组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.