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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex question ...

Status
Not open for further replies.

rtb1

IS-IT--Management
Jul 17, 2000
73
ES
Hi guys,

I’m looking for a regex solution for this:

I would like to check if the contents of a $ matches part of the contents of another $. If so proceed with the next action.

Example:

$one = “regular expressions”;
$two = “I would like to know more about regular expressions”;

match is true

Is the next sort of matching possible (contents of $one matches part of $two with characters between the matching parts in $two)?

$one = “reg ex”;
$two = “I would like to know more about regular expressions”;

match is true

Anyhelp would be great!

Raoul
[sig][/sig]
 
Hi Raoul,

First one:
Code:
$one = 'regular expressions';
$two = 'I would like to know more about regular expressions';

if($two =~ /$one/){
    print "match found\n";
}
That will print 'match found', it searches for $one in $two.

Second one:

Hmmm... goBoating? [sig]<p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>Making mistakes, so you don't have to. &lt;grin&gt;[/sig]
 
Why sure.......<grin> by the way Mike, what is Queen's English? <chuckle>

$one = “reg ex”;
$two = “I would like to know more about regular expressions”;

A period is a regex wild card. So, we can ask for the first part, 'reg' and then some wild card stuff, and then the second part, 'ex'.

So for the specific example....

$two =~ /reg.*?ex/;

the * says multiples of the previous char (which was a wild card) and the ? says match minimally.

A little tricker......if you did not have your pattern in two pieces and you wanted to be able to explode the parts of your pattern automatically.......

$two = “I would like to know more about regular expressions”;
$one = 'like know about reg ex';
@pieces = split(/ /,$one);
$pattern_to_match = join '.*?',@pieces;
# If I thought that through correctly, it should build a string that looks like -
# 'like.*?know.*?about.*?reg.*?ex' - which would look for the words with
# wild cards in between.
# So,
$two =~ /$pattern_to_match/;

# I would [red]like[/red] to [red]know[/red] more [red]about[/red] [red]reg[/red]ular [red]ex[/red]pressions

hope this helps....

[sig]<p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo[/sig]
 
Great, thanks!

So short and so powerfull.

I have been experimenting with this and it works great. Probably there is no solution for this but maybe there is. In my country a lot of words have special signs, like: í, ó, ñ etc. However people are not consistent at all in writing those words correctly. As I already assumed &quot;i&quot; doesn't match &quot;í&quot;.

Is there a solution for this one so a search for 'mañana' will match 'manana'?

Anyway, thanks for the help both,

Raoul [sig][/sig]
 
/reg.*?ex/

Hmmm --- sort of obvious really, I wasn't paying attention was I...

And the Queens English, btw, is wot I speek... Just for your information... [sig]<p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>Making mistakes, so you don't have to. &lt;grin&gt;[/sig]
 
Hi Tom,

I didn't try this yet but I was thinking to do something in the line of replacing special characters like:

$one=~ s/í/\i/g;

(probably this won't work like this but to show the idea)

The above command would be needed for each special character. I was just thinking if it wouldn't slow down the script to much when there is a lot to check.

Is this what you mean with creating a dictionary or (if not) would that be faster?

Regards,

Raoul [sig][/sig]
 
I started to experiment with this and apparantly it does work in some cases.

$one=~ s/í/\i/g;

and

$one=~ s/ó/\o/g;

both work, but:

$one=~ s/ñ/\n/g; $one=~ s/é/\e/g; $one=~ s/á/\a/g;

either replaces the character with a &quot; &quot; or with a square character? The character itself is identified but it is not replaced by the indicated character.

How is this possible or more important how do I solve this situation?

Raoul [sig][/sig]
 
\n is a newline. Why are you escaping the replacement character?

$one=~ s/ñ/n/g; $one=~ s/é/e/g; $one=~ s/á/a/g;

I'm not exactly sure how to do it, but you should be able to group these all into one statement so that your string only needs to be parsed once for all of the characters instead of once for each one. I don't have a lot of time to think about it right now, but I'm sure one of the other members could help you with that. [sig]<p> Sincerely,<br><a href=mailto: > </a><br><a href= Anderson</a><br>CEO, Order amid Chaos, Inc.<br>
[/sig]
 
I got this piece of code from this forum and I guess it was necessary for that example to escape the replacement character.

Thank you for correcting this,

Raoul [sig][/sig]
 
#!perl.exe
$one = &quot;reg ex&quot;;
$two = &quot;I would like to know more about regular expressions&quot;;
#print &quot;$one\n$two\n&quot;;
@seperate_words = split /\W/, $one;
foreach $word (@seperate_words)
{
$regex = $regex . $word . &quot;(\\w)*(\\W)*&quot;;
# print &quot;$word\n&quot;;
}
#print &quot;\n\n$regex\n&quot;;
if( $two =~ m/$regex/ )
{
print &quot;Yep\n&quot;;
}
else
{
print &quot;Nope\n&quot;;
}
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top