-2
/^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\(\s*\S+\s*\))/;

can anyone help me in understanding what the above line will do in a perl script?

I am not getting any output with the above code and wish to modify it. That's why want to understand it's function.

TLP
  • 66,756
  • 10
  • 92
  • 149
  • The above line is a regex match operator. Used alone, it will try and match against the content of `$_`, and store captured values in `$1`, `$2`, etc. Basically it is looking for a string like `1 2 3 4 5 (6)`. – TLP Apr 07 '23 at 04:14

1 Answers1

1

The regex is a complicated way of breaking something up on whitespace (\s+) and keeping the non-whitespace groups (\S+). Notice that it's just a repeating pattern. This is the job for split:

 my @words = split /\s+/, $string;

So, what that regex means is that the creator didn't read Learning Perl. ;)

But, to really help you, we need to see the string you want to break up and what you expect to get out it.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • Given the optional whitespace sections, this will not be equivalent to `split /\s+/`, or even `split ' '`. There are escaped parentheses that were hidden by the lack of formatting in the question. If you can have whitespace inside those, then you cannot use whitespace as delimiter with split. Unless perhaps if you use the LIMIT option. – TLP Apr 07 '23 at 14:44
  • I'll update the answer when the OP shows some sample input data. But even with parens, I would still use split, then handle the last bit in parens separately. – brian d foy Apr 08 '23 at 02:46
  • I'll agree that sample input data is essential to answer the question correctly. I am just arguing on basis that the regex is actually correctly reflecting what we might expect from the input. In which case the regex would give 6 captures, and split might give 9. – TLP Apr 08 '23 at 09:52
  • Thanks for helping out with the code explanation. – Aakriti Goel Jun 12 '23 at 15:16