regex question

I

inderpaul_s

I have the following code...my only question is in line 4. where you
see the ^ and $ (last one) character which means start/end of the
string. But my question is where does it stop matching for the caret
and the dollar sign ?

I think the caret is everything from the beginning of the string all
the way to the end and same when matching from the end of the line it
matches everything to the start of the string. There is nothing to tell
it to stop ? Does this sound correct ?


1. my $line = "jsmith:password \n";
2. my $user = "jsmith";
3. my $password = "password";

4. if($line =~ /^$user:$password$/)

5. {print "user name and password match\n";}
6. else{print "user name and password does NOT match\n";}
 
P

Paul Lalli

I have the following code...my only question is in line 4. where you
see the ^ and $ (last one) character which means start/end of the
string. But my question is where does it stop matching for the caret
and the dollar sign ?

I don't understand what you're asking. What does "stop matching" mean?
There is no start or stop to the matching. The ^ matches exactly once
- at the beginning of the string. The $ matches exactly once - at the
end of the string [1].
I think the caret is everything from the beginning of the string all
the way to the end and same when matching from the end of the line it
matches everything to the start of the string.

No, the caret matches ONLY the beginning of the string. It does not
match any characters that follow the beginning of the string. Same
applies to the $ for the end.
There is nothing to tell
it to stop ? Does this sound correct ?

No, because there is no concept of "stopping". I think you are
confusing the ^ and $ anchors with the + or * quantifiers.
4. if($line =~ /^$user:$password$/)

This says: "If $line matches the beginning of the string, the variable
$user, a colon, the variable $password, and the end of the string".
That is, $line must contain *nothing* but $user, colon, $password.
Nothing can come before or after that, or the pattern match fails.

Paul Lalli
 
X

xhoster

I have the following code...my only question is in line 4. where you
see the ^ and $ (last one) character which means start/end of the
string. But my question is where does it stop matching for the caret
and the dollar sign ?

It doesn't. Since every string has a beginning and an end, ^ and $
in isolation will always match.
I think the caret is everything from the beginning of the string all
the way to the end and same when matching from the end of the line it
matches everything to the start of the string.

The ^ matches the start. The $ matches the end, or the newline right
before the end.
There is nothing to tell
it to stop ?

Sure, the rest of the regexp.

Xho
 
D

Dr.Ruud

(e-mail address removed) schreef:
4. if($line =~ /^$user:$password$/)

Both $user and $password could contain 'special' characters like
..*?()[]{}\
so you should use \Q to get those quoted (see perldoc perlre).

4. if($line =~ /^\Q$user:$password$\E/)
 
R

robic0

I have the following code...my only question is in line 4. where you
see the ^ and $ (last one) character which means start/end of the
string. But my question is where does it stop matching for the caret
and the dollar sign ?

I think the caret is everything from the beginning of the string all
the way to the end and same when matching from the end of the line it
matches everything to the start of the string. There is nothing to tell
it to stop ? Does this sound correct ?


1. my $line = "jsmith:password \n";
2. my $user = "jsmith";
3. my $password = "password";

4. if($line =~ /^$user:$password$/)
^^^^^^ ^^^^^^^^^^
The carret at the beggining denotes that the next char
after it must be the first char in the line.
The dollar sign at the end denotes the first previous char
must be at the end.

Thats all it does. Everything inbetween is relative only to each other.
The jist of regexp is that every char is juxta-positioned to every other
character in the matching string. There are various anchor characters
line the ones your using for beginning and end.
 
R

robic0

^^^^^^ ^^^^^^^^^^
The carret at the beggining denotes that the next char
after it must be the first char in the line.
The dollar sign at the end denotes the first previous char
must be at the end.

Thats all it does. Everything inbetween is relative only to each other.
The jist of regexp is that every char is juxta-positioned to every other
character in the matching string. There are various anchor characters
line the ones your using for beginning and end.
I will break down regexp won notch further. The *thing*, no matter
what *it* is (in its primitive state, is a character), in regexp,
it is only relative to whats before it and what comes after it.
Consider the regexp engine as walking through a bunch of *things*
framed by whats before and after the *thing*, then proceeds to the
next *thing*. When you have resolved all the lowest level *thing*'s, you
have to put it into a group. The group becomes the new *thing*.
Wash, rinse and repeat........
 
I

inderpaul_s

ok i understand what your saying...if you start at the beginning of
$line and it contains "jsmith:password" and same if you start at the
end of $line and it also contains the string "jsmith:password" so the
if statement evaulates to true

thanks again to everyone
 
R

robic0

ok i understand what your saying...if you start at the beginning of
$line and it contains "jsmith:password" and same if you start at the
end of $line and it also contains the string "jsmith:password" so the
if statement evaulates to true

thanks again to everyone
Good, though maybe you would refreain from using "regex question" in your
subject line since its used multiple times in this news group.
 
J

John W. Krahn

Dr.Ruud said:
(e-mail address removed) schreef:
4. if($line =~ /^$user:$password$/)

Both $user and $password could contain 'special' characters like
.*?()[]{}\
so you should use \Q to get those quoted (see perldoc perlre).

4. if($line =~ /^\Q$user:$password$\E/)

You have the \E in the wrong place.

$ perl -le' $_ = qr/^password$/; print; $_ = qr/^\Qpassword$\E/; print'
(?-xism:^password$)
(?-xism:^password\
E)


John
 
J

Jürgen Exner

Dr.Ruud said:
(e-mail address removed) schreef:
4. if($line =~ /^$user:$password$/)

Both $user and $password could contain 'special' characters like
.*?()[]{}\
so you should use \Q to get those quoted (see perldoc perlre).

4. if($line =~ /^\Q$user:$password$\E/)

Well, I surely hope the OP expects $user and $password to contain special
characters.

If you want to match literally then
if ($line eq "$user:$password")
would be much easier to write and to read.

jue
 
R

robic0

Dr.Ruud said:
(e-mail address removed) schreef:
4. if($line =~ /^$user:$password$/)

Both $user and $password could contain 'special' characters like
.*?()[]{}\
so you should use \Q to get those quoted (see perldoc perlre).

4. if($line =~ /^\Q$user:$password$\E/)

Well, I surely hope the OP expects $user and $password to contain special
characters.

If you want to match literally then
if ($line eq "$user:$password")
would be much easier to write and to read.

jue
This is correct, but you must tell him why he thinks he needs regexp
and how his regexp is not adequate. Regexp is afterall a notion
that the literal is not adequate, nor trusted.
 
R

robic0

Dr.Ruud said:
(e-mail address removed) schreef:
4. if($line =~ /^$user:$password$/)

Both $user and $password could contain 'special' characters like
.*?()[]{}\
so you should use \Q to get those quoted (see perldoc perlre).

4. if($line =~ /^\Q$user:$password$\E/)

You have the \E in the wrong place.

$ perl -le' $_ = qr/^password$/; print; $_ = qr/^\Qpassword$\E/; print'
(?-xism:^password$)
(?-xism:^password\
E)


John

Here we go again.... The 'what ifs'...
Here's one for you kahnn... What if '\Q','\E' do not cover all escape
characters? Read the docs, they don't!

This is complete, but some char's are not necessary, posted over and over and
over again.... Capitan cut'n paste

sub convertPatternMeta
{
my ($pattern) = shift;
my @regx_esc_codes =
(
"\\", '/', '(', ')', '[', ']', '?', '|',
'+', '.', '*', '$', '^', '{', '}', '@'
);
foreach my $tc (@regx_esc_codes) {
# code template for regex
my $xxx = "\$pattern =~ s/\\$tc/\\\\\\$tc/g;";
eval $xxx;
if ($@) {
# the compiler will show the escape char, add
# it char to @regx_esc_codes
$@ =~ s/^[\s]+//s; $@ =~ s/[\s]+$//s;
die "$@";
}
}
return $pattern;
}
 
J

Jürgen Exner

ok sure...will do for next time. thanks.

What the f*** will you do next time?

Please quote appropriate context -as has been a proven custom in Usenet for
over 2 decades- such that people have a chance to know what you are talking
about.

jue
 
D

Dr.Ruud

John W. Krahn schreef:
Dr.Ruud:
(e-mail address removed):
4. if($line =~ /^$user:$password$/)

Both $user and $password could contain 'special' characters like
.*?()[]{}\
so you should use \Q to get those quoted (see perldoc perlre).

4. if($line =~ /^\Q$user:$password$\E/)

You have the \E in the wrong place.

$ perl -le' $_ = qr/^password$/; print; $_ = qr/^\Qpassword$\E/;
print' (?-xism:^password$)
(?-xism:^password\
E)

Yes. I was first going to leave it out since it is not needed. Than I
inserted it in the wrong position...
 
T

Tad McClellan

ok i understand what your saying...


No you don't. Keep trying.

Anchors (eg. ^ and $ ) do not affect where in the string
you "start" attempting to match.

Anchors match a *position in the string*.


[ Please quote some context in followups like everybody else does.
Please start doing this very soon.
Thank you.
]

if you start at the beginning of
$line


Pattern matching *always* "starts" at the beginning of the string.

and it contains "jsmith:password" and same if you start at the
end of $line


Pattern matching *never* "starts" at the end of the string.

and it also contains the string "jsmith:password" so the
if statement evaulates to true


What "if statement"?

Please quote some context in followups like everybody else does.

/^jsmith:password$/

matches:
beginning of string followed by
the character "j" followed by
the character "s" followed by
the character "m" followed by
...
the character "o" followed by
the character "r" followed by
the character "d" followed by
the end of string (or just before a newline at the end of string)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,295
Latest member
EmilG1510

Latest Threads

Top