Take the 2-minute tour ×

I need to grep the following:
my $string = "Thanks for contacting support. Your ticket number: 123. All the best!"
my $string = "Thanks for contacting support. Your ticket #: 123. All the best!"

How can I grep 123 into variable $ticket_no with PERL?

I think the regexp should be something like this ticket[ \t]+(number|#)?[: \t]+([0-9]+)

Is it correct?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Yes, that regex would work. You could use it like this:

$string =~ m/ticket[ \t]+(number|#)?[: \t]+([0-9]+)/;
my $ticket_no = $2;

Note that you'll want to handle, somehow, the case that the regular expression didn't match, since in that case, $ticket_no will be undefined.

share|improve this answer
Yes, it works perfect. Thank you –  Kirill Reva Mar 29 '12 at 21:53
add comment

As ruakh already said - your regex would work.

As a hint: There are tons of regex test sites out there that make the testing and optimizations of regular expressions easy.

An example would be: http://myregextester.com/

share|improve this answer
add comment

Your Answer


