Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2016-10-22 09:35 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (294 votes). Check out past polls.