Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regex question

Reply
Thread Tools

regex question

 
 
inderpaul_s@yahoo.com
Guest
Posts: n/a
 
      04-13-2006
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 = "jsmithassword \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";}

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      04-13-2006
wrote:
> 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

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      04-13-2006
"" <> wrote:
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      04-13-2006
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/)

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      04-13-2006
On 13 Apr 2006 15:46:07 -0700, "" <> wrote:

>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 = "jsmithassword \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.

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


 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      04-14-2006
On Thu, 13 Apr 2006 16:13:02 -0700, robic0 wrote:

>On 13 Apr 2006 15:46:07 -0700, "" <> wrote:
>
>>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 = "jsmithassword \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.
>
>>
>>5. {print "user name and password match\n";}
>>6. else{print "user name and password does NOT match\n";}

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........
 
Reply With Quote
 
inderpaul_s@yahoo.com
Guest
Posts: n/a
 
      04-14-2006
ok i understand what your saying...if you start at the beginning of
$line and it contains "jsmithassword" and same if you start at the
end of $line and it also contains the string "jsmithassword" so the
if statement evaulates to true

thanks again to everyone

 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      04-14-2006
On 13 Apr 2006 17:19:30 -0700, "" <> wrote:

>ok i understand what your saying...if you start at the beginning of
>$line and it contains "jsmithassword" and same if you start at the
>end of $line and it also contains the string "jsmithassword" 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.
 
Reply With Quote
 
inderpaul_s@yahoo.com
Guest
Posts: n/a
 
      04-14-2006
ok sure...will do for next time. thanks.

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      04-14-2006
Dr.Ruud wrote:
> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57