Syed Jafri
Syed Jafri

Reputation: 476

perl regex for everything between global

I have strings formatted as the following.

could/MD be/VB said/VBN to/TO be/VB ``/`` duly/RB adopted/VBN ''/'' 
even/RB though/IN 
[ it/PRP ]

I want to match the thing after / like MD, VB, VBN, TO, VB, ", RB etc.

This is what I have

while(<$FH>)
{
  if($_ =~ /\/(.+?)\s/g)
  {
    print "$1\n";
  }
}

If I run it using the above string I get.

MD
RB
PRP

It is only matching the first instance of my regex even though I am using /g How can I match every single one, including the punctuation?

Upvotes: 0

Views: 30

Answers (1)

hwnd
hwnd

Reputation: 70722

You can iterate through each line as follows:

while (<$FH>) {
  print "$_\n" for /\/(\S+)/g;
}

Ideone Demo

Upvotes: 2

Related Questions