Schnulli
Schnulli

Reputation: 41

Matches without matching alternations

I want to filter some substrings from a string:

.
.
th>Di, -12 ;0907<th
th>Mi, -44 ;1007<th
th>Do, -66 ;1107<th
.
.

My perl script is:

.
.
my (@d,$h,$s);
@d=$h=~/(th>(Di|Mi|Do), -\d\d ;\d\d\d\d<th)/igs;
foreach $s (@d)
   {print  "$s\n";};  

Output is:
th>Di, -12 ;0907<th
Di
th>Mi, -44 ;1007<th
Mi
th>Do, -66 ;1107<th
Do

My problem, I need this output:

th>Di, -12 ;0907<th
th>Mi, -44 ;1007<th
th>Do, -66 ;1107<th

How can I filter matches without alternations within the regex?

Upvotes: 0

Views: 80

Answers (2)

hwnd
hwnd

Reputation: 70732

If you know that your data has the same amount of numbers in the following lines, you could do..

(th>(?:Di|Mi|Do),\s+-\d{2}\s+;\d{4}<th)

If the length of your numbers vary, then I would do.

(th>(?:Di|Mi|Do),\s+-\d+\s+;\d+<th)

Instead of using a space in your regex, use \s:

\s+          matches whitespace (\n, \r, \t, \f, and " ") (1 or more times)

Reason being, if your data has more than one space, then regular expression will not match the following:

th>Di,  -12  ;0907<th
th>Mi,   -44  ;1007<th 

Upvotes: 1

raina77ow
raina77ow

Reputation: 106443

Well, the easiest way would be just to turn a capturing group into a non-capturing one with ?: regex operator:

@d=$h=~/(th>(?:Di|Mi|Do), -\d\d ;\d\d\d\d<th)/igs;

Actually, ?: often is used with |: to limit the alternation's scope, you have to use parenthesis, but you don't have to leverage their main effect (capturing) too.

Upvotes: 4

Related Questions