TopCoder
TopCoder

Reputation: 4296

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";
}

Upvotes: 0

Views: 321

Answers (2)

MPękalski
MPękalski

Reputation: 7103

Try this one, worked for me in Excel

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

Result for the given string:

test-app-ref-Man_pub_aut_art_1234

Upvotes: 0

jb.
jb.

Reputation: 10351

looks like you're expecting the string to end with 4 digits, but your string actually ends with 4 digits, then a semi-colon. add a semi-colon before the $

Upvotes: 3

Related Questions