What does \. mean when matching in Perl? (backslash dot) [duplicate] - regex

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
I am wondering what the characters \. mean in Perl, specifically in a matching expression. I know the \ can be an escape character. Is it simply escaping the dot? Or does it have an additional meaning together?
In the context below, I am assuming that when the condition ($ARGV[$i] =~ /\./) is satisfied, the variable $Chain is not set to the argument $ARGV[$i]. I tried looking up information on Perl regular expressions and matching but I am having trouble fitting the context.
for (my $i = 0; $i <= $#ARGV; $i++) {
if ($ARGV[$i] && ! ($ARGV[$i] =~ /\./)) {
$Chain .= " " . $ARGV[$i];
}
}

It's escaping the period so that it can match a period instead of using the period's usual special meaning.

Related

Trying to understand regex snippet (/[-/\\^$*+?.()|[\]{}]/g, '\\$&') [duplicate]

This question already has answers here:
Difference between $1 and $& in regular expressions
(3 answers)
Closed 2 years ago.
I am most interested in learning more about that piece at the end, '\\$&'
I'm not really sure what its doing, or how it works, but it gets the job done.
The code that I have:
function escapeRegExp(s) {
return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
}
const searchRegex = new RegExp(
searchQuery
.split(/\s+/g)
.map(s => s.trim())
.filter(s => !!s)
.map(word => `(?=.*\\b${escapeRegExp(word)})`).join('') + '.+',
'i'
)
$& is defined in MDN's String#replace -- Specifying a string as a parameter reference as
Pattern $& Inserts the matched substring.
Essentially, this gives you back the entire matched substring. In your example, \\ prepends a single backslash, effectively escaping the match.
Here are some examples:
// replace every character with itself doubled
console.log("abc".replace(/./g, "$&$&"));
// replace entire string with itself doubled
console.log("abc".replace(/.*/g, "$&$&"));
// prepend a backslash to every match
console.log("abc".replace(/./g, "\\$&"));
// behavior with capture groups that can be accessed with $1, $2...
console.log("abc".replace(/(.)(.)/g, "[ $$1: $1 ; $$2: $2 ; $$&: $& ]"));

How to get values inside of brackets according to BODMAS rule [duplicate]

This question already has answers here:
PHP regular expression to replace nested () with []
(5 answers)
Closed 4 years ago.
I tried and stuck to this formula which only only work on 1 pair of brackets:
preg_match('#\((.*?)\)#', $text, $match);
print $match[1];
What I want to do is parse the text after the last open parenthesis before the first close parenthesis.
For example, I have equation like this:
1+(2+(3+(5-6)))+(7-8)
The text that would be parsed is "5-6".
Match ( > anything except ( or ) > )
$text = '1+(2+(3+(5-6)))+(7-8)';
preg_match('/\(([^\(\)]+)\)/', $text, $match);
var_dump($match);

What does s/(\W)/\\$1/g do in perl? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I went over a piece of code in which a subroutine takes video filename as argument, and then printing it's duration time. Here I'm only showing the snippet.
sub videoInfo {
my $file = shift;
$file =~ s/(\W)/\\$1/g;
}
So far I understood is that it is dealing with whitespaces but I'm not able to break the meaning of code, I mean what is $1 and how it will work?
It puts backslashes in front of non-word characters. Things like "untitled file" becomes "untitled\ file".
As in most regular expression operations $1 represents the first thing captured with (...) which in this case is the (\W) representing a single non-word character.
I think this is an unnecessary home-rolled version of quotemeta.

Weird behaviour of the global g regex flag [duplicate]

This question already has answers here:
Help understanding global flag in perl
(2 answers)
Closed 9 years ago.
my $test = "There was once an\n ugly ducking";
if ($test =~ m/ugly/g) {
if ($test =~ m/here/g) {
print 'Match';
}
}
Results in no output, but
my $test = "There was once an\n ugly ducking";
if ($test =~ m/here/g) {
if ($test =~ m/ugly/g) {
print 'Match';
}
}
results in Match!
If I remove the g flag from the regex, then the second internal test matches whichever way around the matches appear in $test. I can't find a reference to why this is so.
Yes. That behaviour is documented in perlop man page. Using m/.../ with g flag advances in the string for the next match.
In scalar context, each execution of "m//g" finds the next match, returning true if it matches, and false if there is no further match. The position after the last match can be read or set using the "pos()" function; see "pos" in perlfunc. A failed match normally resets the search position
to the beginning of the string, but you can avoid that by adding the "/c" modifier (e.g. "m//gc"). Modifying the target string also resets the search position.
So, in first case after ugly there isn't any here substring, but in second case it first matches here in There and later it finds the ugly word.

how do you match two strings in two different variables using regular expressions?

$a='program';
$b='programming';
if ($b=~ /[$a]/){print "true";}
this is not working
thanks every one i was a little confused
The [] in regex mean character class which match any one of the character listed inside it.
Your regex is equivalent to:
$b=~ /[program]/
which returns true as character p is found in $b.
To see if the match happens or not you are printing true, printing true will not show anything. Try printing something else.
But if you wanted to see if one string is present inside another you have to drop the [..] as:
if ($b=~ /$a/) { print true';}
If variable $a contained any regex metacharacter then the above matching will fail to fix that place the regex between \Q and \E so that any metacharacters in the regex will be escaped:
if ($b=~ /\Q$a\E/) { print true';}
Assuming either variable may come from external input, please quote the variables inside the regex:
if ($b=~ /\Q$a\E/){print true;}
You then won't get burned when the pattern you'll be looking for will contain "reserved characters" like any of -[]{}().
(apart the missing semicolons:) Why do you put $a in square brackets? This makes it a list of possible characters. Try:
$b =~ /\Q${a}\E/
Update
To answer your remarks regarding = and =~:
=~ is the matching operator, and specifies the variable to which you are applying the regex ($b) in your example above. If you omit =~, then Perl will automatically use an implied $_ =~.
The result of a regular expression is an array containing the matches. You usually assign this so an array, such as in ($match1, $match2) = $b =~ /.../;. If, on the other hand, you assign the result to a scalar, then the scalar will be assigned the number of elements in that array.
So if you write $b = /\Q$a\E/, you'll end up with $b = $_ =~ /\Q$a\E/.
$a='program';
$b='programming';
if ( $b =~ /\Q$a\E/) {
print "match found\n";
}
If you're just looking for whether one string is contained within another and don't need to use any character classes, quantifiers, etc., then there's really no need to fire up the regex engine to do an exact literal match. Consider using index instead:#!/usr/bin/env perl
#!/usr/bin/env perl
use strict;
use warnings;
my $target = 'program';
my $string = 'programming';
if (index($string, $target) > -1) {
print "target is in string\n";
}