Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this line of Perl meaningless? s/^(\d+)\b/$1/sg

Tags:

regex

perl

Does this line of Perl really do anything?

$variable =~ s/^(\d+)\b/$1/sg;

The only thing I can think of is that $1 or $& might be re-used, but it is immediately followed by.

$variable =~ s/\D//sg;

With these two lines together, is the first line meaningless and removable? It seems like it would be, but I have seen it multiple times in this old program, and wanted to make sure.

like image 754
700 Software Avatar asked Feb 08 '12 15:02

700 Software


People also ask

What does S * mean in Perl?

Substitution Operator or 's' operator in Perl is used to substitute a text of the string with some pattern specified by the user.

What is the meaning of $1 in Perl regex?

$1 equals the text " brown ".

What is S in Perl regex?

The substitution operator, s///, is really just an extension of the match operator that allows you to replace the text matched with some new text. The basic form of the operator is − s/PATTERN/REPLACEMENT/;

How do you check if a line contains a string in Perl?

The built in Perl operator =~ is used to determine if a string contains a string, like this. The !~ operator is used to determine if a string does not contains a string, like this. Often, variables are used instead of strings.


2 Answers

$variable =~ s/^(\d+)\b/$1/sg;
  • The anchor ^ at the beginning makes the /g modifier useless.
  • The lack of the wildcard character . in the string makes the /s modifier useless, since it serves to make . also match newline.
  • Since \b and ^ are zero-width assertions, and the only things outside the capture group, this substitution will not change the variable at all.

The only thing this regex does is capture the digits into $1, if they are found.

The subsequent regex

$variable =~ s/\D//sg;

Will remove all non-digits, making the variable just one long number. If one wanted to separate the first part (matched by the first regex), the only way to do so would be by accessing $1 from the first regex.

However, the first regex in that case would be better written simply:

$variable =~ /^(\d+)\b/;

And if the capture is supposed to be used:

my ($num) = $variable =~ /^(\d+)\b/;
like image 164
TLP Avatar answered Sep 23 '22 03:09

TLP


Is "taint mode" in use? (Script is invoked with -T option.)

Maybe it's used to sanitize (i.e. untaint) user input.

like image 25
Mikel Avatar answered Sep 23 '22 03:09

Mikel