Determining if end of one string is the same as beginning of another (with or without regex; Java)

83 Views Asked by At

I want to match the beginning of one string with the end of another string, I could not find a solution on the internet, let's say I have these sequences:

I have to sequences:

seq1 = AAATAAA
seq2 = AAATTTT

I want to check if a part of the end of seq1 matches a part of the beginning of seq2. A test like this:
:

AAATAAA
    AAATTTT  //--> so I want to match AAA

//and also check the other way around:
AAATTTT
       AAATAAA //-->no match

I tried to convert a string to a completely optional regex like so:

(A?A?A?T?T?T?T?)$ //from seq1
(A?A?A?T?A?A?A?)$ //from seq2

However this won't work properly:

(A?A?A?T?T?T?T?)$ //--> matches AAA = correct  
(A?A?A?T?A?A?A?)$ //--> matches T   = not correct!

This could be solved using a regex that only allows matches if the preceeding characters are matched(if possible). So if this is possible it will not match (A?A?A?T?A?A?A?)$ because AAAis not matched. I would like to fix this issue using regex however solutions which do not use regex are welcome too!

0

There are 0 best solutions below