Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > pattern matching

Reply
Thread Tools

pattern matching

 
 
LiHui
Guest
Posts: n/a
 
      04-20-2004
Can someone tell me what does this line do ?

$line =~ m/^\|\s*\w\S*\s*(?:\|.+?){10,}\|$/o

I know that it check to see if the line begin with "|" follow by
whitespace, word, nonwhitespace and than I'm lost. What is s*(?:\|.+?)

Any help will be greatly appreciate. Thanks LH
 
Reply With Quote
 
 
 
 
Brad Baxter
Guest
Posts: n/a
 
      04-20-2004
On Mon, 19 Apr 2004, LiHui wrote:

> Can someone tell me what does this line do ?
>
> $line =~ m/^\|\s*\w\S*\s*(?:\|.+?){10,}\|$/o
>
> I know that it check to see if the line begin with "|" follow by
> whitespace, word, nonwhitespace and than I'm lost. What is s*(?:\|.+?)


Make that "|" followed by optional whitespace, a word character, optional
nonwhitespace, optional whitespace, ten or more of "these": |x..., and
ending in "|".

Also, it's not s*(?:\|.+?), it's \s*(?:\|..+?). (?:...) are grouping (not
capturing) parentheses. They're followed by {10,} so you want 10 or more
of those groups. Each group is "|" followed by at least one, possibly
more, character(s) that match(es) /./ but not "|" (because +? is
non-greedy).

Below is an expanded (using /x) version:

# example line that will match
my $line = '| abc |0|1|2|3|4|5|6|7|8|9|x|y|z|';

$line =~ m/^\|\s*\w\S*\s*(?:\|.+?){10,}\|$/o and print "yes\n";

$line =~ m/
^ # begins with
\| # 'or' bar
\s* # optional whitespace
\w # ONE word character
\S* # optional nonwhitespace
\s* # optional whitespace
(?: # begin the group
\| # 'or' bar
.+ # at least one character
? # make the '+' non-greedy
) # end the group
{10,} # give me 10 or more GROUPS
\| # 'or' bar
$ # at the end
/ox and print "yes\n";


I assume you've looked at perldoc perlre.

Regards,

Brad
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      04-20-2004
Brad Baxter wrote:
>
> On Mon, 19 Apr 2004, LiHui wrote:
>
> > Can someone tell me what does this line do ?
> >
> > $line =~ m/^\|\s*\w\S*\s*(?:\|.+?){10,}\|$/o
> >
> > I know that it check to see if the line begin with "|" follow by
> > whitespace, word, nonwhitespace and than I'm lost. What is s*(?:\|.+?)

>
> Make that "|" followed by optional whitespace, a word character, optional
> nonwhitespace, optional whitespace, ten or more of "these": |x..., and
> ending in "|".
>
> Also, it's not s*(?:\|.+?), it's \s*(?:\|..+?). (?:...) are grouping (not
> capturing) parentheses. They're followed by {10,} so you want 10 or more
> of those groups. Each group is "|" followed by at least one, possibly
> more, character(s) that match(es) /./ but not "|" (because +? is
> non-greedy).
>
> Below is an expanded (using /x) version:
>
> # example line that will match
> my $line = '| abc |0|1|2|3|4|5|6|7|8|9|x|y|z|';
>
> $line =~ m/^\|\s*\w\S*\s*(?:\|.+?){10,}\|$/o and print "yes\n";
>
> $line =~ m/
> ^ # begins with
> \| # 'or' bar
> \s* # optional whitespace
> \w # ONE word character
> \S* # optional nonwhitespace
> \s* # optional whitespace
> (?: # begin the group
> \| # 'or' bar
> .+ # at least one character
> ? # make the '+' non-greedy
> ) # end the group
> {10,} # give me 10 or more GROUPS
> \| # 'or' bar
> $ # at the end
> /ox and print "yes\n";
>
> I assume you've looked at perldoc perlre.


Also, the /o option is not required as there are no variables in the
regular expression.

perldoc perlop


John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
LiHui
Guest
Posts: n/a
 
      04-21-2004
Thanks Brad & John. Got it now.

LiHui
 
Reply With Quote
 
Scott J
Guest
Posts: n/a
 
      07-07-2004
On 19 Apr 2004 18:58:30 -0700, (LiHui)
wrote:

>Can someone tell me what does this line do ?
>
>$line =~ m/^\|\s*\w\S*\s*(?:\|.+?){10,}\|$/o
>
>I know that it check to see if the line begin with "|" follow by
>whitespace, word, nonwhitespace and than I'm lost. What is s*(?:\|.+?)
>
>Any help will be greatly appreciate. Thanks LH


I'll give it a go My regex abilities are a bit rusty.

^\| line starts with |
\s* followed by 0 or more whitespaces
\w followed by an alphanumeric character
\S* followed by 0 or more non whitespaces
\s* followed by 0 or more whitespaces
(?:\|.+?){10,} is a quantified extended regex sequence (see below)
\|$ line ends with |

o switch tells the pattern to compile only once.

Quantified regex sequence :
(?:...) is a cluster only parenthesis, no capturing (thanks Camel
book) which I think means that the pattern matches, but does not store
the matched string in a variable. The remainder of this sequence is a
regular regex :
\| matches |
..+? matches one character, 1 or more times (minimally)

{10,} tells the pattern inside the () to match at least 10 times

Does that help or hinder?

Scott
 
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
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
Matching neighbouring words of a pattern using Regex CV Perl 2 08-31-2004 12:27 AM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM
Pattern matching help! grep emails from file! danpres2k Perl 3 08-25-2003 02:47 PM
A newbie question on pattern matching DelphiDude Perl 3 07-26-2003 12:54 PM



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