Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why `stoutest` is not a valid regular expression?

Tags:

regex

perl

From perlop:

If "/" is the delimiter then the initial m is optional. With the m you can use any pair of non-whitespace characters as delimiters. This is particularly useful for matching path names that contain "/", to avoid LTS (leaning toothpick syndrome). If "?" is the delimiter, then the match-only-once rule of ?PATTERN? applies. If "'" is the delimiter, no interpolation is performed on the PATTERN. When using a character valid in an identifier, whitespace is required after the m.

So I can pick up any letter as a delimiter. Eventually this regex should be fine:

stoutest

That can be rewritten

s/ou/es/

However it does not seems to work in Perl. Why?

$ perl -e '$_ = qw/ou/; stoutest; print'
ou
like image 395
nowox Avatar asked Aug 29 '15 22:08

nowox


People also ask

What is not a valid regular expression?

The JavaScript exception "invalid regular expression flag" occurs when the flags in a regular expression contain any flag that is not one of: g , i , m , s , u , y or d . It may also be raised if the expression contains more than one instance of a valid flag.

What is a valid regular expression?

What is RegEx Validation (Regular Expression)? RegEx validation is essentially a syntax check which makes it possible to see whether an email address is spelled correctly, has no spaces, commas, and all the @s, dots and domain extensions are in the right place.

Should I avoid using RegEx?

When Not to Use Regex. Regular expressions can be a good tool, but if you try apply them to every situation, you'll be in for a world of hurt and confusion down the line. Using them is essentially like injecting a small programming language into another programming language.

Should we use regular expression?

Regular expressions are particularly useful for defining filters. Regular expressions contain a series of characters that define a pattern of text to be matched—to make a filter more specialized, or general. For example, the regular expression ^AL[.]* searches for all items beginning with AL.


2 Answers

Because Perl can't pick out the operator s

perldoc perlop says this

Any non-whitespace delimiter may replace the slashes. Add space after the s when using a character allowed in identifiers.

This program works fine

my $s = 'bout';
$s =~ s toutest;
say $s;

output

best
like image 98
Borodin Avatar answered Sep 23 '22 02:09

Borodin


Because stoutest, or any other string of alphanumeric characters, is a single token in the eyes of the Perl parser. Otherwise we couldn't use any barewords that begin with s (or m, or q, or y).

This works, though

$_ = "ou";
s toutest;
print
like image 37
mob Avatar answered Sep 23 '22 02:09

mob