Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Matching bits of 2 strings

by Anonymous Monk
on Jan 22, 2002 at 13:49 UTC ( #140627=note: print w/ replies, xml ) Need Help??


in reply to Matching bits of 2 strings

My solution to the problem isn't as compact as the other solutions, but TIMTOWTDI!

#! /usr/local/bin/perl $string1 = "the date is today"; $string2 = "the date is tomorrow"; $concat = $string1 . "#" . $string2; if ( $concat =~ m/^(.*)(.*)#\1(.*)$/ ) { print "matching part: $1\n"; print "difference: $2\ndifference: $3\n"; } else { print "strings $string1 and $string2 do not match!\n"; }
When run it generates the following output
matching part: the date is to difference: day difference: morrow
I have to note that this solutions uses the fact dat the to strings doesn't contain any poundsymbols (#). When one of the to strings contain a poundsymbol, you have to use another symbol(sequence) to, for example:
... $concat = $string1 . "#!!#" . $string2; if ( $concat =~ m/^(.*)(.*)#!!#\1(.*)$/ ) { ...
I hope that this will give you a hint to construct your own solution!


Comment on Re: Matching bits of 2 strings
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://140627]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2015-07-02 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (46 votes), past polls