Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl regex exclude optional word from match

Tags:

regex

perl

I have a strings and need to extract only icnnumbers/numbers from them.

icnnumber:9876AB54321_IN
number:987654321FR
icnnumber:987654321YQ

I need to extract below data from above example.

9876AB54321
987654321FR
987654321YQ

Here is my regex, but its working for first line of data.

(icnnumber|number):(\w+)(?:_IN)

How can I have expression which would match for three set of data.

like image 732
vkk05 Avatar asked Dec 06 '22 08:12

vkk05


1 Answers

Given your strings to extract are only upper case and numeric, why use \w when that also matches _?

How about just matching:

#!/usr/bin/env perl

use strict;
use warnings;

while (<DATA>) {
   m/number:([A-Z0-9]+)/;
   print "$1\n";
}

__DATA__
icnnumber:9876AB54321_IN
number:987654321FR
icnnumber:987654321YQ
like image 54
Sobrique Avatar answered Dec 19 '22 21:12

Sobrique