Re: regexp question

2009-09-05 Thread John W. Krahn

Noah Garrett Wallach wrote:


Okay I am having troubles finding this. in the perldoc modules.
Is there a slicker way to write the following?

if ($line =~ /(Blah1)(.*)/) {


At this point we know that the pattern matched so $1 will contain the 
string Blah1 and $2 will contain a string of zero or more non-newline 
characters.



 $blah = $1 if $1;


The test is redundant because we already know that Blah1 is true.


 $blah2 = $2 if $2;


This only assigns the string in $2 to $blah2 if it is not  or 0.

}   



John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: regexp question

2009-09-04 Thread Chas. Owens
On Fri, Sep 4, 2009 at 04:08, Noah Garrett Wallachnoah-l...@enabled.com wrote:

 Hi there,

 is there any way to search for the following text?  In some cases the text
 that I am search could be

 one-two-three-

 or sometimes the text could be

 one-two-

 what is a nice easy why to parse the above - quotes not included.
snip

What you are looking for is quantifiers.  Quantifiers state how many
times the preceding pattern must match:

if (/^one-two-(?:three-){0,1}$/} {
print matched\n
}

In that example the quantifer {0,1} says that the pattern (?:three-)
should be between zero and one times.  This is a common enough
quantifier that it has gotten a shortcut:

if (/^one-two-(?:three-)?$/} {
print matched\n
}

Other shortcuts are + (for {1,}, i.e. one or more) and * (for {0,},
i.e. zero or more).

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: regexp question

2009-09-04 Thread Erez Schatz
2009/9/4 Noah Garrett Wallach noah-l...@enabled.com:

 is there any way to search for the following text?  In some cases the text
 that I am search could be

 one-two-three-
 or sometimes the text could be one-two-
If you're looking for this specific text then a good answer was
already given, but if that's an example, and what you want is
someword-otherword-anotherone- then you may need something more
generic like
/(?:[a-z]+-){2,3}/

which looks for a series of characters followed by a - that recurrs 2
or 3 times.

-- 
Erez

The government forgets that George Orwell's 1984 was a warning, and
not a blueprint
http://www.nonviolent-conflict.org/ -- http://www.whyweprotest.org/

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: regexp question

2009-09-04 Thread Chas. Owens
On Fri, Sep 4, 2009 at 23:06, Noah Garrett Wallachnoah-l...@enabled.com wrote:


 Okay I am having troubles finding this. in the perldoc modules.
 Is there a slicker way to write the following?

        if ($line =~ /(Blah1)(.*)/) {
             $blah = $1 if $1;
             $blah2 = $2 if $2;
        }
snip

In list context a regex will return its captures:

my ($blah, $blah2) = $line =~ /(Blah1)(.*)/;


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Regexp-Question: What is backtracking?

2009-03-12 Thread Erez Schatz
2009/3/12 Deviloper devilo...@slived.net
 Can somebody explain what Backtracking is?

 thanx,
 B.


In a nutshell, consider the following regex: /foo((b+)ar)/
a regex engine will check every character in the string that is
checked against until it reaches the first f. When reached, it will
mark the place and check the next character, if its not an 'o', the
mark will be removed. If it is, the next character will be checked and
so on. When the string's end will be reached, it will report whether a
match was found (and where it was found).

Now, in Perl, we would like to use that ((b+)ar) as $1, $2 and so on.
This means, that when the engine needs to mark not only the location
of the 'f', but also the 'b', when reached. Assuming the string
'snafoobbbar', the engine will need to backtrack over to the first
'b', and each time save the 'b+' string for future reference.

When the first 'b' is reached, $1 is now (assuming a match) 'b' and $2
is (potentially) 'bar'. The next character is also a 'b', and with
that $1 is now 'bb' and $2 (potentially) 'bbar'. The engine have to
make those adjustments accordingly, with each iteration forcing it to
readjust all the variables it saves.

I do recommend J. Freidl's Mastering Regular Expressions for a
better understanding of how those engines function, at least
theoretically (as the actual algorithmic implementation might differ
much from my basic explanation, or from the descriptions in the book).

-- 
Erez

It's time to grow out of the browser; we have nothing to lose but our metaphors.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Regexp question...

2001-12-21 Thread John W. Krahn

Wim De Hul wrote:
 
 Hey folks,

Hello,

 I'm writen a script to manage my router configuration.
 To get the IOS version I use regular expressions.
 First, I did it with:
 
 $_ = getparameter($sysdescr_mib,System description);# Get
 the SysDescr via SNMP
 /Version /;
 $_ = $';
 (/, /);
 $swversion = $`;
 
 But this is not the way I want it to do... I like to have more
 professional code.
 Is there a way to do it in one line (probably there is)?
 
 I tried with:
 
 $_ = snmpget($sysdescr);
 $IOS_version = /Version (.*?)\, /i;
 
 When I do now: print $1\n; I get the IOS version
 But when I type print $IOS_version\n; It displays just 1...
 Can I get rid of the S_?

my $IOS_version = $1 if snmpget( $sysdescr ) =~ /Version\s+([^,]+),/i;



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Regexp question...

2001-12-21 Thread John W. Krahn

Andrea Holstein wrote:
 
 Wim De Hul wrote:
 
  I tried with:
 
  $_ = snmpget($sysdescr);
  $IOS_version = /Version (.*?)\, /i;
 
  When I do now: print $1\n; I get the IOS version
  But when I type print $IOS_version\n; It displays just 1...
  Can I get rid of the S_?
 
 A matching always return a list of the captured items.
 On the left side of the assignment is a scalar,
 so list in a scalar context returns its size.
 ^
While this is true that is _not_ what is happening in this case.

$ perl -le'$data = one two three four five six;
$test = $data =~ /(\S+)\s+(\S+)\s+(\S+)/;
print Test 1: $test;
($test) = $data =~ /(\S+)\s+(\S+)\s+(\S+)/;
print Test 2: $test;
$test = $data =~ /(\S+)\s+(\S+)\s+(\S+)/g;
print Test 3: $test;
($test) = $data =~ /(\S+)\s+(\S+)\s+(\S+)/g;
print Test 4: $test;
$test = () = $data =~ /(\S+)\s+(\S+)\s+(\S+)/;
print Test 5: $test;
'
Test 1: 1
Test 2: one
Test 3: 1
Test 4: four
Test 5: 3


perldoc perlop

[snip]
   m/PATTERN/cgimosx

   /PATTERN/cgimosx
   Searches a string for a pattern match, and in
   scalar context returns true if it succeeds, false
   if it fails.


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Regexp question...

2001-12-21 Thread Michael R. Wolf

John W. Krahn [EMAIL PROTECTED] writes:

 $ perl -le'$data = one two three four five six;

 $test = $data =~ /(\S+)\s+(\S+)\s+(\S+)/;
 print Test 1: $test;

 ($test) = $data =~ /(\S+)\s+(\S+)\s+(\S+)/;
 print Test 2: $test;

 $test = $data =~ /(\S+)\s+(\S+)\s+(\S+)/g;
 print Test 3: $test;

 ($test) = $data =~ /(\S+)\s+(\S+)\s+(\S+)/g;
 print Test 4: $test;

 $test = () = $data =~ /(\S+)\s+(\S+)\s+(\S+)/;
 print Test 5: $test;
 '

 Test 1: 1
 Test 2: one
 Test 3: 1
 Test 4: four
 Test 5: 3

It took a while for Test 4 to work in my brain.  (I don't
have Perl running to test it out -- I'm assuming this is
real output).

Programming Perl (PP) says:
PP The /g modifier specifies global pattern matching - that is,
PP matching as many times as possible within the string. How it
PP behaves depends on the context. In a list context, it
PP returns a list of all the substrings matched by all the
PP parentheses in the regular expression. If there are no
PP parentheses, it returns a list of all the matched strings,
PP as if there were parentheses around the whole pattern.
PP 
PP In a scalar context, m//g iterates through the string,
PP returning true each time it matches, and false when it
PP eventually runs out of matches. (In other words, it
PP remembers where it left off last time and restarts the
PP search at that point. You can find the current match
PP position of a string using the pos function - see Chapter
PP 3.) If you modify the string in any way, the match position
PP is reset to the beginning.

The sneaky (clever) part to me was the m//g in scalar
context.  The first time it was called (test 3) it returned
qw(one two three).  The second time it was called (on the
same string) it started where it left off and returned
qw(four five six) which is used to assign in a list context
to the LHS.  There being only one scalar in the list to
fill, it gets the first element - four.

Thanks.


-- 
Michael R. Wolf
All mammals learn by playing!
   [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: regexp question

2001-11-03 Thread Jeff 'japhy' Pinyan

On Nov 3, Martin Karlsson said:

Could anyone give me a push in the right direction, please?
How do I write a regexp that matches double instances of letters (in a
file i read into a variable), e.g. aa LL pp etc. ?

Is there an easy way, or must I use
   (aa|bb|cc|... and so on ) ?

You want to use a backreference:

  if ($string =~ /([a-zA-Z])\1/) {
# $1 is the character that was doubled
  }

-- 
Jeff japhy Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for Regular Expressions in Perl published by Manning, in 2002 **


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: regexp question

2001-11-03 Thread Martin Karlsson

Thanks Jeff! Works like a charm!

Be well!
/Martin
* Jeff 'japhy' Pinyan ([EMAIL PROTECTED]) wrote:
 On Nov 3, Martin Karlsson said:
 
 Could anyone give me a push in the right direction, please?
 How do I write a regexp that matches double instances of letters (in a
 file i read into a variable), e.g. aa LL pp etc. ?
 
 Is there an easy way, or must I use
  (aa|bb|cc|... and so on ) ?
 
 You want to use a backreference:
 
   if ($string =~ /([a-zA-Z])\1/) {
 # $1 is the character that was doubled
   }
 
 -- 
 Jeff japhy Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
 RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
 ** Look for Regular Expressions in Perl published by Manning, in 2002 **
 
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]

-- 

Martin Karlsson [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: regexp question

2001-10-23 Thread register

my $string = 'pgrap.C4A2S1R.1.gif';

my @s = ($string =~ /(?:\.)*(\w+)(?:\.)*/g);

the arraytores the elements

the //g makes it find more than one match ... post again if you need more
explanation ...
hth

On Tue, Oct 23, 2001 at 06:20:31AM -0500, Jerry Preston shaped the electrons to read:
 Hi,
 
 I want to break down this name, pgrap.C4A2S1R.1.gif, into 4 parts.  I
 can do it with split:
 
   ( $a1,$a2,$a3,$a4 ) = split( /\./, $program );
 
 How do I do it with   s/\b(\.)\b(\.)\b(\.)\b(\.)/?
 
 Thanks,
 
 Jerry
 
 
 
 
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: regexp question

2001-10-23 Thread Richard_Cox

On 23 October 2001 12:21 Jerry Preston [mailto:[EMAIL PROTECTED]] wrote:
 I want to break down this name, pgrap.C4A2S1R.1.gif, into 4 parts.  I
 can do it with split:
 
   ( $a1,$a2,$a3,$a4 ) = split( /\./, $program );
 
 How do I do it with   s/\b(\.)\b(\.)\b(\.)\b(\.)/?
 


You can't, since:

1. that regex of word boundary (i.e. non-word-character (\W) followed by
word-character (\w) or visa versa) followed by a period 4 times can't occur
('' doesn't contain any word boundaries).

2. a substituion (s///) needs a regex (between the first and second
delimeters) and then some to replace the match with (between the second and
third[1])

What do you actually want to do here?

If you want to replace part of the input string a subsitution is likely to
be the right thing to do, OTOH something like (untested)

my @parts = split(/\./, $program);
$parts2 = 'fred';
$pragram = join('.',@parts);

might be easier to understand; the same in a substitution would be something
like (also untested)

$program =~ s!(?=\.)[^.]+!fred!;

(The match part of which is: match non-periods that follow a period.)


Richard Cox 
Senior Software Developer 
Dell Technology Online 
All opinions and statements mine and do not in any way (unless expressly
stated) imply anything at all on behalf of my employer




[1] Or 3rd and 4th if using matched separators as in s{}.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: regexp question

2001-10-22 Thread Andrea Holstein

Jerry Preston wrote:
 
 I want to break down this name, pgrap.C4A2S1R.1.gif, into 4 parts.  I
 can do it with split:
 
   ( $a1,$a2,$a3,$a4 ) = split( /\./, $program );
 
 How do I do it with   s/\b(\.)\b(\.)\b(\.)\b(\.)/?
 

Don't use substutions when you want to get parts.
It's a job for pattern matching
(if you really don't want to split).

( $a1,$a2,$a3,$a4 ) = $program =~ m/^(.+)\.(.+)\.(.+)\.(.+)$/;

Greetings,
Andrea

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: regexp question

2001-10-22 Thread Andrea Holstein

Andrea Holstein wrote:
 
 ( $a1,$a2,$a3,$a4 ) = $program =~ m/^(.+)\.(.+)\.(.+)\.(.+)$/;
 
Oh, I see, it's a slow solution.
Substitute every (.+) with ([^\.]+) and the pattern matching will be
quite quicker.

Greetings,
Andrea

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: regexp question

2001-10-21 Thread Andrea Holstein

Birgit Kellner wrote:
 
 my $header = Joe DoebrbrThe book I wrote yesterday;
 my $title;
 if ($header =~ /(^\S.+)(brbr)(\S.+$)/m) { $title = $1: $3;}
 print $title\n;
 
 Is there a shorter, simpler, more efficient way to do this? I still need
 $header later on as it is.
 
I try to go another way as the good answers alreay resolved.

1. There's no need to capture brbr.
So 1st optimization is
if ($header =~ /(^\S.+)brbr(\S.+$)/m { $title = $1: $2 }

2. The .+ will first go till to end of line.
Then the regex machine will go character for character backward.
There are two possible ways to speed up:
a) my @headerparts = split /brbr/, $header;
   if (@headerparts == 2 and 
   $headerparts[0] =~ /^\S.+$/ and 
   $headerparts[1] =~ /^\S.+/) 
   {
   $title = $headerparts[0]: $headerparts[1];
   }

   (I'm not sure, wether .+ is still needed.
It stands for everything except the \n.
If there are no newlines in,
it's quicker and simpler to test:
my @headerparts = split /brbr/, $header;
if (@headerparts == 2 and
$headerparts[0] =~ /^\S./ and
$headerparts[1] =~ /^\S./)
{
   $title = $headerparts[0]: $headerparts[2];
}

b) I could imagine, that the title doesn't contain any html tags.
So, you could tell the regex machine to don't backtrack unnecessary.

if ($header =~ /(^\S[^]+)brbr(\S.+$)/m) { $title = $1: $3; }

Good Luck,
Andrea

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: regexp question

2001-10-19 Thread Martin

try this:
my $title =~ s/brbr/  /i;
if ($header =~ /(^\S.+)(brbr)(\S.+$)/m)
{
$header =~ s/brbr//i;
}
print $header;

hope it helps
martin



birgit kellner wrote:

 my $header = Joe DoebrbrThe book I wrote yesterday;
 my $title;
 if ($header =~ /(^\S.+)(brbr)(\S.+$)/m) { $title = $1: $3;}
 print $title\n;
 
 Is there a shorter, simpler, more efficient way to do this? I still need 
 $header later on as it is.
 
 I quite often have to copy the value of a variable and then run a regexp 
 substitution on it; I was wondering whether there's a better approach 
 than this one.
 
 Birgit Kellner
 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: regexp question

2001-10-19 Thread Jeff 'japhy' Pinyan

On Oct 19, birgit kellner said:

my $header = Joe DoebrbrThe book I wrote yesterday;
my $title;
if ($header =~ /(^\S.+)(brbr)(\S.+$)/m) { $title = $1: $3;}
print $title\n;

Is there a shorter, simpler, more efficient way to do this? I still need 
$header later on as it is.

You could do:

  ($title = $header) =~ s/brbr/: /;

That's a bit generalized -- I figure it will work as you expect it to in
all cases.

-- 
Jeff japhy Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for Regular Expressions in Perl published by Manning, in 2002 **


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: regexp question

2001-10-19 Thread birgit kellner

--On Freitag, 19. Oktober 2001 12:49 -0400 Jeff 'japhy' Pinyan 
[EMAIL PROTECTED] wrote:

 On Oct 19, birgit kellner said:

 my $header = Joe DoebrbrThe book I wrote yesterday;
 my $title;
 if ($header =~ /(^\S.+)(brbr)(\S.+$)/m) { $title = $1: $3;}
 print $title\n;

 Is there a shorter, simpler, more efficient way to do this? I still need
 $header later on as it is.

 You could do:

   ($title = $header) =~ s/brbr/: /;

 That's a bit generalized -- I figure it will work as you expect it to in
 all cases.

I had thought of that, but believed that it will also perform the 
substitution on $header. I just tested it, and that doesn't seem to be the 
case. If that's true, there's the solution to my problem.

Birgit Kellner

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Regexp question

2001-06-25 Thread Me

 [matching web page links]
 [using regexes]

Don't use regexes. They aren't the right tools for the task.

Use one of the cpan modules for parsing web pages.
Some are written specifically for pulling out links.

http://search.cpan.org/




Re: Regexp question

2001-06-25 Thread Tim Wood

Drats - just when I got the regexp worked out too...
 $_=~ m/(A)(.*?)( \/A)/

  [matching web page links]
  [using regexes]

 Don't use regexes. They aren't the right tools for the task.

 Use one of the cpan modules for parsing web pages.
 Some are written specifically for pulling out links.

 http://search.cpan.org/



Re: Regexp question

2001-06-25 Thread Me

 Drats - just when I got the regexp worked out too...
  $_=~ m/(A)(.*?)( \/A)/

Kudos for working out the regex that works given your
assumptions. If the web pages you will be parsing are
known to be constrained to the assumptions you've
established, then you're done.

But be aware that your regex will fail on some web pages.
And tightening up the regex to cope with the exceptions
rapidly becomes a futile exercise. Experienced perl
coders don't use regexes for most recursive and most
hierarchical parsing tasks, such as parsing html or xml.

This has been discussed an infinite number of times on
many perl lists for several years. Some recent experimental
regex extensions begin to break down this barrier to use of
regexes for recursive and hierarchical formats, but they are
still very much experimental, and are still pointless in the
particular cases of html and xml given the available cpan
modules.

But, as always, timtowtdi...

   [matching web page links]
   [using regexes]
 
  Don't use regexes. They aren't the right tools for the task.
 
  Use one of the cpan modules for parsing web pages.
  Some are written specifically for pulling out links.
 
  http://search.cpan.org/




Re: Regexp question

2001-06-25 Thread Brett W. McCoy

On Tue, 26 Jun 2001, Tim Wood wrote:

 Drats - just when I got the regexp worked out too...
  $_=~ m/(A)(.*?)( \/A)/

Until you get to the one that is like

a href=some/url
  a bunch of stuff
/a

Which is why the parser module is a good idea -- it can handle
non-idiomatic cases like this.

-- Brett
   http://www.chapelperilous.net/btfwk/

Visit beautiful Vergas, Minnesota.




Re: regexp question

2001-06-07 Thread Michael Fowler

On Thu, Jun 07, 2001 at 08:25:05AM -0700, Martin Weinless wrote:
 take the regexp '.n+..?.?v*.'
 
 By all that is sacred, if we use the string 'supernova', there should be
 no match since there are too many characters before the 'n'

Nope.  Consider

supern   ova
^^   ^
.n+.?.?v*.

.?.?v* is all optional, and the regex engine opts out of using those
patterns in order to match the entire string.  Your pattern is also not
anchored, so the match can start anywhere.


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--



Re: regexp question

2001-06-07 Thread Peter Scott

At 08:25 AM 6/7/2001 -0700, Martin Weinless wrote:
take the regexp '.n+..?.?v*.'

By all that is sacred, if we use the string 'supernova', there should be
no match since there are too many characters before the 'n'

However, any regexp checking code will report a match.

Atom by atom:

. -  match a character.  Will match the 's' to begin with.
n+ - match 'n' one or more times.  Since the second character isn't a 'n'' 
it has to advance the pointer until the . matches the 'r'.
. - match a character.   Matches 'o'.
.? - match a character 0 or 1 times.  Will match 'v' to begin with (greedy).
.? - match a character 0 or 1 times.  Will match 'a' to begin with.
v* - match 'v' 0 or more times. We're at the end of the string initially, 
so we match none.
. - match a characer.  Oops, there aren't any.  Never mind, we have some 
backtracking we can try.  First one is to take that last .? and match 0 
instead of 1 character.  Now the v* matches 0 characters and the final . 
matches the 'a'.  Success!




RE: regexp question

2001-06-07 Thread Wagner-David

Since you are not anchoring in way or another(\b or ^ or \W), it meets your
criteria of any character then n then 

Wags ;)

-Original Message-
From: Martin Weinless [mailto:[EMAIL PROTECTED]]
Sent: Thursday, June 07, 2001 08:25
To: [EMAIL PROTECTED]
Subject: regexp question


Hello,

This is probably not a true beginners question - it refers to an example
that I use in class when we discuss regexps.

Here is the problem:

take the regexp '.n+..?.?v*.'

By all that is sacred, if we use the string 'supernova', there should be
no match since there are too many characters before the 'n'

However, any regexp checking code will report a match.

Any ideas/comments/etc.

Thanks

Marty W.

WINDOWS: 32 bit extensions and a graphical shell for a 16 bit patch to an
8 bit operating system originally coded for a 4 bit microprocessor,
written by a 2 bit company that can't stand 1 bit of competition.




Re: Regexp Question Again

2001-05-11 Thread M.W. Koskamp


- Original Message -
From: Gross, Stephan [EMAIL PROTECTED]
To: 'Beginner Perl' [EMAIL PROTECTED]
Sent: Friday, May 11, 2001 5:26 PM
Subject: Regexp Question Again


 I wasn't clear last time.  I wrote:
 I want to match the following:
 1) the letters PT
 2) a space or nothing
 3) a word that may or may not be in parentheses or even not exist
 and return item #3 (which may be null)
 Example:
 PT (XYZ) or PT XYZ or PTXYZ or PT
 should return XYZ except the last case, which should return .

 I can do everything except the parentheses case. Any ideas?

 The problem is the final parenthesis.  If I use (.*) or (\w*) then it
 swallows the final parenthesis.  I want to discard it if it is present.
 Also, the XYZ term may include spaces.

 None of the solutions you guys sent do this.  Willing to try again?

 Thanks again.
i tried

/PT\s*\(?(.*?)\)?/i
But making .* non greedy kinda spoils it. It will work with a non greedy .*
but that will eat the closing right bracket.
Following works, but doesnt deserver any price for beauty

for (PT (XYZ), PT XYZ, PTXYZ, PT){
 $me = $_ ;
 $me=~s/PT|\s|\(|\)//g;
 print have:$me\n;
}

 ___
 Stephan Gross   Loral Skynet 908-470-2388
 mailto:[EMAIL PROTECTED] [EMAIL PROTECTED]







Re: Regexp Question Again

2001-05-11 Thread Paul


--- Gross, Stephan [EMAIL PROTECTED] wrote:
 I wasn't clear last time.  I wrote:
 I want to match the following:
 1) the letters PT
 2) a space or nothing
 3) a word that may or may not be in parentheses or even not exist
 and return item #3 (which may be null)
 Example:
 PT (XYZ) or PT XYZ or PTXYZ or PT
 should return XYZ except the last case, which should return .
 I can do everything except the parentheses case. Any ideas?
  
 The problem is the final parenthesis.  If I use (.*) or (\w*) then it
 swallows the final parenthesis.  I want to discard it if it is
 present. Also, the XYZ term may include spaces.

swallows? As in, it gets included in the pattern?
of (\w*) 

Could you elaborate, and/or post an example?

__
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/