Partial matching of a regular expression 
Author Message
 Partial matching of a regular expression

Is there a way in perl to "partially match" on input ?

For example, with expression /hello/ and input "hel", I'd like to know
that the regular expression is currently matching, but not done yet.



Sun, 08 Feb 2004 22:05:46 GMT  
 Partial matching of a regular expression
: Is there a way in perl to "partially match" on input ?

: For example, with expression /hello/ and input "hel", I'd like to know
: that the regular expression is currently matching, but not done yet.

This may not be what you need, but sometimes you want to turn the test
around.

        $input = <>;

        print "partial match" if 'hello' =~ m/^${input}$/;



Sun, 08 Feb 2004 23:49:53 GMT  
 Partial matching of a regular expression

Quote:


>    print "partial match" if 'hello' =~ m/^${input}$/;

You probably don't want that final "$" there.

Regards,

Ian



Mon, 09 Feb 2004 09:24:28 GMT  
 Partial matching of a regular expression

Quote:

> Is there a way in perl to "partially match" on input ?

> For example, with expression /hello/ and input "hel", I'd like to know
> that the regular expression is currently matching, but not done yet.

"Partially matching" might not really do what you want it to.

Basically any string will partially match /hello/ - they'll all match
the position before the h, and in a few cases, more than that. I
gather what you would really like is for the string to "partially
match", and for all of the string to be used in the match. That means
that you are really considering patterns of the form /^hello$/ (i.e.
the full string must be able to match the pattern).

If you are happy to use patterns that are basically constant strings,
you can reverse the pattern and string as another poster described
(anchored at the start, but not at the end). I don't know of any
simple way of checking for "partial matches" with more complex
regexes. You might be able to take the pattern as a string to start
with, and then process it to produce a new pattern that itself matches
any "partial match". That would be quite difficult to do in general,
though, considering how baroque perl regexes can get.

You could also process the pattern string to embed code to be executed
during the matching process, to allow you to check at each point if
the whole string had been used, and to set a flag if so. I'll leave
that as an exercise to the reader. There's also some interface to the
regex engine guts around, but I don't recall the name of the module
required to use it (probably "re" or something like that...) - I'm
sure with enough ingenuity that could be used to implement "partial
matches".

Regards,

Ian



Mon, 09 Feb 2004 10:44:27 GMT  
 Partial matching of a regular expression

Quote:

> This may not be what you need, but sometimes you want to turn the test
> around.

>    $input = <>;

>    print "partial match" if 'hello' =~ m/^${input}$/;

This works for plain strings, but you can't use metacharacters.

What I actually want to do is match an expression by adding one
character at a time (not a whole string) and poll the matcher to see
if the string still could match.

I don't think you can do this in Perl (TNWTDI :) ) short of writing
a matcher. Or maybe fooling around with the regular expression.



Mon, 09 Feb 2004 14:12:11 GMT  
 Partial matching of a regular expression

: > This may not be what you need, but sometimes you want to turn the test
: > around.
: >
: >  $input = <>;
: >
: >  print "partial match" if 'hello' =~ m/^${input}$/;

: This works for plain strings, but you can't use metacharacters.

use quotemeta to convert metachars into plain old text.

Another poster also pointed out a bug in my code - the trailing $
should not be there.

: What I actually want to do is match an expression by adding one
: character at a time (not a whole string) and poll the matcher to see
: if the string still could match.

: I don't think you can do this in Perl (TNWTDI :) ) short of writing
: a matcher. Or maybe fooling around with the regular expression.

Well nothing can be done in any language, short of writing code or fooling
around with expressions. ;)



Mon, 09 Feb 2004 18:32:50 GMT  
 Partial matching of a regular expression

Quote:

> Is there a way in perl to "partially match" on input ?

> For example, with expression /hello/ and input "hel", I'd like to know
> that the regular expression is currently matching, but not done yet.

use re 'eval'; # this is lexically scoped.  Be careful with it!
my $regex = join '', map { $_ . qq/(?{ print "letter $_\n" })/ },
        split //, "hello";
"hel" =~ /^$regex/;

--
"I think not," said Descartes, and promptly disappeared.



Tue, 17 Feb 2004 08:56:49 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. regular expression matching using a scalar variable

2. regular expression | match

3. Regular expression to match up parentheses (()())

4. Regular Expression matching sentence

5. Putting variables inside m// (regular expression matching)

6. regular expression matching (a better diff) again...

7. regular expression matching source code: help reqeust.

8. Regular Expressions: Greedy Matching

9. OT: regular expression matching multiple occurrences of one group

10. Help needed with tough regular expression matching

11. matching patters wi ns within an array of regular expressions

 

 
Powered by phpBB® Forum Software