简体   繁体   English

Perl正则表达式以匹配模式

[英]Perl regex to match pattern

I need to match a string something like 我需要匹配一个类似的字符串

$filecontents = test-app-ref-Man_pub_aut_art_1234;

My regex is something like this but it doesn't work: Can someone help me with the regex what I am doing wrong here. 我的正则表达式是这样的,但它不起作用:有人可以用正则表达式来帮助我,我在这里做错了什么。 Please note that I am reading the complete file as a string and this is one of the strings. 请注意,我正在以字符串形式读取完整的文件,这是字符串之一。

while($filecontents =~ m/(test)(-|_)(.*)(_\d{4,})$/isgm){
    print " String10 : '$1$2$3$4'\n";
}

looks like you're expecting the string to end with 4 digits, but your string actually ends with 4 digits, then a semi-colon. 看起来您期望字符串以4位数字结尾,但是您的字符串实际上以4位数字结尾,然后是分号。 add a semi-colon before the $ $之前添加分号

Try this one, worked for me in Excel 试试这个,在Excel中为我工作

(-|_)?.*_\d{4}

Result for the given string: 给定字符串的结果:

test-app-ref-Man_pub_aut_art_1234 test-app-ref-Man_pub_aut_art_1234

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

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