Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2017-12-16 04:25 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (448 votes). Check out past polls.