简体   繁体   English

使用Perl正则表达式提取匹配项

[英]Extracting matches with Perl regex

I am trying to extract the following string RL2.OPT.TEST.01 from this one a lot of text...[RL2.OPT.TEST.01]<some more text . 我试图从这一个a lot of text...[RL2.OPT.TEST.01]<some more text提取以下字符串RL2.OPT.TEST.01 a lot of text...[RL2.OPT.TEST.01]<some more text Basically there can be anything before the RL2. 基本上RL2.之前可以有任何东西RL2. but it always begins with RL2. 但它始终以RL2.开头RL2. , and always finish by either ] , < , or a space. ,并始终通过]<或空格完成。

I tried with the following regex: m/RL2\\..*[\\s<\\]]+?$/g but even if it finds a match for the string -- [RL2.OPT.TEST.01] , it does not work for -- [RL2.OPT.TEST.01] some more text . 我尝试使用以下正则表达式: m/RL2\\..*[\\s<\\]]+?$/g但即使找到字符串匹配-- [RL2.OPT.TEST.01] ,它-- [RL2.OPT.TEST.01]不适合-- [RL2.OPT.TEST.01] some more text

I need an array of all the resultings matches in the large string I have. 我需要一个包含大字符串中所有结果匹配的数组。 I think I should also mention that this string has a lot of newline characters, but never in the middle of the strings I am trying to extract. 我想我还应该提一下,这个字符串有很多换行符,但从不在我试图提取的字符串中间。

Any clue about what is wrong with my regex? 关于我的正则表达式有什么问题的任何线索?

Use a negated character class and remove the end of the line anchor $ 使用否定的字符类并删除行锚$的结尾

m/RL2\.[^\s<\]]*/g

DEMO DEMO

[^\\s<\\]]* Negated character class which matches any character but not of space or < or ] zero or more times. [^\\s<\\]]*与任何字符匹配但不匹配空格或<]零次或多次的否定字符类。

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

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