Reputation: 607
When I run the below program, it gave me nothing ("") as result.
Scenario 1:-
$var = "A STITCH IN TIME SAVES NINE";
if ($var =~ /[1..9]/i) {
print "FOUND\n"
}
But when I add space before and after '..' operator, it threw " Found and is " as a result.
Scenario :- 2
$var = "A STITCH IN TIME SAVES NINE";
if ($var =~ /[1 .. 9]/i) {
print "FOUND\n"
}
Could anyone explain me what is happening here??
Upvotes: 0
Views: 146
Reputation: 57640
The regex language is embedded in Perl (and vice versa), but it shares no syntax with Perl¹. This means other sytax for repetition or ranges.
1) Regexes share syntax with Perl strings, although the two are not fully compatible, see different meanings of the \b
escape.
Character classes define a set of multiple properties. The character class will match if one of the specified properties will match. A charclass can contain:
[aeiou]
(match lowercase vowels)[A-Z]
(uppercase latin characters)[^']
(everything that is not a single quote)\d
, \w
(and a lot of fun with Unicode properties)If a charclass contains a character multiple times, this is irrelevant, as it behaves like a set union.
The metacharacters in charclasses are
]
: end of match. To match square brackets, one has to do [\[\]]
or [][]
, as a charclass cannot be empty.^
which is only special in leading position: [~&|^]
would match any of the bitwise logical Perl operators.-
. To match a literal minus, it can be put at the end of the charclass: The class [+-*]
would be invalid (*
comes before +
, so the class is empty, which is illegal), but [+*-]
works just fineSpace is significant inside charclasses, even under the /x
flag.
[1..9]
could also be written as [19.]
, and matches a 1
, 9
or .
, as the period is not a metacharacter inside charclasses.[1 .. 9]
could be written [19. ]
, and additionally matches a space. As I said above, whitespace is significant in charclasses.If you want to match any of the digits 0
or 1
to 9
, you can use the range [0-9]
. Remember that the minus is the range operator in charclasses.
Upvotes: 1
Reputation: 5090
The ".." does not indicate a range in your character class, it indicates a full stop (twice :-)). You don't have either a 1 , full-stop or 9 in your string, so it does not match first time round.
Also, you are not populating $i anywhere. $i is just a regular scalar variable, it does not get magically populated with anything. The i flag on the end of your regular-expression (/i) means case-insensitive search (in case you thought that was populating $i
Upvotes: 4
Reputation: 944175
When I run the below program, it gave me nothing ("") as result.
The string does not contain a 1, a full stop, or a 9. The pattern does not match anything in the string.
But when I add space before and after '..' operator, it threw " Found and is " as a result
It does contain a multiple spaces. When you expand your character class to match against to include spaces, it matches.
I have also include "$i" in the statement, but still it did not showed any value.
You never define $i
. You only try to read from it.
Upvotes: 5