Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

matching two regular expresion?

Status
Not open for further replies.

grimbys

Programmer
Dec 19, 2002
34
0
0
ES
I want to match this two expresions:

(this is the phrase and no the expresion regular)
(DET|The) (NUM|14) (PREP|of) (MONTH|January)

with:
The NUM of MONTH (of YEAR)?

A global example:
(TYPE | word) (TYPE | word) ...

whith
(TYPE |word )*
Any idea?
Thanks!
 
If I understand you, you can use something like
[tt]my $s = '(DET|The) (NUM|14) (PREP|of) (MONTH|January)';
my %data;

while( $s =~ /
\( # opening literal parenthesis of duplet
( # colect to $1
[^|]+ # up to a pipe
) # stop collecting $1
\| # a literal pipe separater
( # collect to $2
[^\)]+ # up to a closing parenthesis
) # stop collecting $2
\) \s* # closing literal parenthesis and optional whitespace
/gx ) {
$data{$1} = $2;
}
print join( ' ', $data{DET}, $data{NUM}, $data{PREP}, $data{MONTH} ), "\n";[/tt]

to parse your string into a hash keyed on TYPE. If a type can reoccur, you will need to modify the code accordingly.

If I have not understood you, maybe you can find what you want from the example anyway!

Yours, "As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought. Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilk
 
Thanks you. I think this is too much specific problem ...
I want to match this two expresions:

I want any form thath consider the first sentence how a expresion regular.
So, if I have:

$s = "(DET|The) (NUM|14) (PREP|of) (MONTH|January|February)"

I look up about a form thath:

(I'd like change =~ signification, by example creating a function)
$s =~ /The 14 of January/ => True!!! ;)
$s =~ /The 14 of February/ => True!!! ;)
$s =~ /DET 14 PREP January/ => True!!! ;)
$s =~ /The NUM of MONTH/ => True!!! ;)
$s =~ /The NUM of MONTH( of YEAR)?/ => True!!! ;)

Obviously this is imposible in Perl, but How can I do a function thath works fine (whithout a combinatorial order)?

I think it is a difficult issue. (is it issue correct here?)

Thanks
 
I need to leave now - I'll try and get back to you tomorrow. "As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought. Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilk
 
grimbys, you were close, just backwards. You need to test if each string matches a given pattern, not if a given pattern fits a string.
Code:
my $s = "(DET|The) (NUM|14) (PREP|of) (MONTH|January|February)";

my $search = "DET 14 PREP January";

print "Match!\n" if($search =~ /$s/);
I don't know if what you need is sometimes more complicated than that, but it works with the example. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Thanks you, icrf ....

See my example at second post:

$s =~ /The NUM of MONTH( of YEAR)?/ => True!!! ;)
(of curse too):
$s =~ /The NUM of MONTH($regex)?/ => True!!!

With your form ($search = "The NUM of MONTH ( of YEAR)?) this don't run.

I think I had to have a combinatory order algoritm to resolve this problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top