Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > partially matching a regexp

Reply
Thread Tools

partially matching a regexp

 
 
Thomas Koenig
Guest
Posts: n/a
 
      08-05-2004
Assume I have a regexp, /^(hello)|(goodbye)$/ for example.

I want to see wether a particular string matches part of that
particular regexp, so "", "h", "he", "hel", "hell", "hello", "g",
"go", "goo" "good", "goodb", "goodby" and "goodbye" would be ok,
and anything else wouldn't.

I could hand-craft this example easily enough, but it grows
tedious and error-prone for more general regular expressions,
and automation would be much preferred.

Ideas?
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      08-05-2004


Thomas Koenig wrote:
> Assume I have a regexp, /^(hello)|(goodbye)$/ for example.
>
> I want to see wether a particular string matches part of that
> particular regexp, so "", "h", "he", "hel", "hell", "hello", "g",
> "go", "goo" "good", "goodb", "goodby" and "goodbye" would be ok,
> and anything else wouldn't.
>
> I could hand-craft this example easily enough, but it grows
> tedious and error-prone for more general regular expressions,
> and automation would be much preferred.
>
> Ideas?


Take a look at the source of File::Stream. A very similar problem is
solved in File::Stream::find and the solution to that problem could
probably be easily be adapted (simplified!) to solve your problem.

 
Reply With Quote
 
 
 
 
Andrew Palmer
Guest
Posts: n/a
 
      08-06-2004

"Thomas Koenig" <> wrote in message
news:cettfn$4o9$...
> Assume I have a regexp, /^(hello)|(goodbye)$/ for example.
>
> I want to see wether a particular string matches part of that
> particular regexp, so "", "h", "he", "hel", "hell", "hello", "g",
> "go", "goo" "good", "goodb", "goodby" and "goodbye" would be ok,
> and anything else wouldn't.
>
> I could hand-craft this example easily enough, but it grows
> tedious and error-prone for more general regular expressions,
> and automation would be much preferred.
>
> Ideas?


Something like:

if(test($input,"hello") || test($input,"goodbye"))
{
# stuff
}

sub test
{
my($s1,$s2)=@_;
for my $len(0..length($s2))
{
return 1 if(substr($s2,0,$len) eq $s1);
}
return 0;
}



 
Reply With Quote
 
Charles DeRykus
Guest
Posts: n/a
 
      08-06-2004
In article <cettfn$4o9$>,
Thomas Koenig <> wrote:
>Assume I have a regexp, /^(hello)|(goodbye)$/ for example.
>
>I want to see wether a particular string matches part of that
>particular regexp, so "", "h", "he", "hel", "hell", "hello", "g",
>"go", "goo" "good", "goodb", "goodby" and "goodbye" would be ok,
>and anything else wouldn't.
>
>I could hand-craft this example easily enough, but it grows
>tedious and error-prone for more general regular expressions,
>and automation would be much preferred.
>
>Ideas?


Sounds as if 'index' (perldoc -f index) might be easier and
in some cases faster than a regex:


my $substring = ...
for ( qw/hello goodbye/ ) {
print "$substring matched $_\n" if index($_, $substring) != -1;
}

--
Charles DeRykus
 
Reply With Quote
 
Jay Tilton
Guest
Posts: n/a
 
      08-08-2004
Thomas Koenig <> wrote:

: Assume I have a regexp, /^(hello)|(goodbye)$/ for example.
:
: I want to see wether a particular string matches part of that
: particular regexp, so "", "h", "he", "hel", "hell", "hello", "g",
: "go", "goo" "good", "goodb", "goodby" and "goodbye" would be ok,
: and anything else wouldn't.
:
: I could hand-craft this example easily enough, but it grows
: tedious and error-prone for more general regular expressions,
: and automation would be much preferred.

This sounds like an opportunity to abuse Perl's
(?(condition)pattern) regex feature.

#!/perl
use strict;
use warnings;

my $pat_h = buildpattern( 'hello' );
my $pat_g = buildpattern( 'goodbye' );

while(<DATA> ) {
chomp;
print "matched '$1' in '$_'\n"
if /$pat_h/ or /$pat_g/;
}

sub buildpattern {
my @lets = $_[0] =~ /./g;
my $pat;
for( 0 .. $#lets ) {
$pat .= $_ == 0 ? $lets[$_] :
$_ == 1 ? "($lets[$_])?" :
"((?($_)$lets[$_]))?"
}
return qr/(^$pat)/;
}

__DATA__
hello
helpme
haveaniceday
goodbye
goodgrief
gabbagabbahey

 
Reply With Quote
 
Thomas Koenig
Guest
Posts: n/a
 
      08-09-2004
Brian McCauley <> wrote:

>> Assume I have a regexp, /^(hello)|(goodbye)$/ for example.
>>
>> I want to see wether a particular string matches part of that
>> particular regexp,


>Take a look at the source of File::Stream. A very similar problem is
>solved in File::Stream::find and the solution to that problem could
>probably be easily be adapted (simplified!) to solve your problem.


I'm currently doing that, and trying to understand what YAPE::regexp
does (which isn't too easy

If I get anywhere, I'll let the newsgroup know.
 
Reply With Quote
 
Jeff 'japhy' Pinyan
Guest
Posts: n/a
 
      08-09-2004
On Mon, 9 Aug 2004, Thomas Koenig wrote:

>Brian McCauley <> wrote:
>
>>> Assume I have a regexp, /^(hello)|(goodbye)$/ for example.
>>>
>>> I want to see wether a particular string matches part of that
>>> particular regexp,

>
>>Take a look at the source of File::Stream. A very similar problem is
>>solved in File::Stream::find and the solution to that problem could
>>probably be easily be adapted (simplified!) to solve your problem.

>
>I'm currently doing that, and trying to understand what YAPE::regexp
>does (which isn't too easy


I'd suggest switching over to Regexp:arser. I'm hoping to get
Regexp::Explain out in the near future.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
RPI Corporation Secretary % have long ago been overpaid?
http://japhy.perlmonk.org/ %
http://www.perlmonks.org/ % -- Meister Eckhart


 
Reply With Quote
 
Andrew Palmer
Guest
Posts: n/a
 
      08-11-2004

"Andrew Palmer" <> wrote in message
news:HpCQc.3093$...
>
> "Thomas Koenig" <> wrote in message
> news:cettfn$4o9$...
> > Assume I have a regexp, /^(hello)|(goodbye)$/ for example.
> >
> > I want to see wether a particular string matches part of that
> > particular regexp, so "", "h", "he", "hel", "hell", "hello", "g",
> > "go", "goo" "good", "goodb", "goodby" and "goodbye" would be ok,
> > and anything else wouldn't.
> >
> > I could hand-craft this example easily enough, but it grows
> > tedious and error-prone for more general regular expressions,
> > and automation would be much preferred.
> >
> > Ideas?

>
> Something like:
>
> if(test($input,"hello") || test($input,"goodbye"))
> {
> # stuff
> }
>
> sub test
> {
> my($s1,$s2)=@_;
> for my $len(0..length($s2))
> {
> return 1 if(substr($s2,0,$len) eq $s1);
> }
> return 0;
> }
>


Clearly the above code is retarded. The equivalent using index() is simply:

if(index("hello",$input)==0 || index("goodbye",$input)==0)
{
# stuff
}

The regex tokenizing modules seem unnecessary for the example you posted. Is
your actual test significantly more complicated?




 
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
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
java.util.regexp: matching square brackets enrique Java 3 02-08-2005 06:57 PM
Regexp question: Trouble matching with backslash Prabh Java 2 05-12-2004 11:10 PM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 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