问题
I have a line of DNA code and I'm trying to use a Java regex expression to match the codon (3 letter sequence) to an amino acid. Below is an example of one of the patterns:
Pattern A = Pattern.compile(("gct")||("gcc")||("gca")||("gcg"));
This syntax does not seem to be working with or without the round brackets. Ultimately the aim of the code is to count the number of times the amino acid is found in the DNA string, and since there are 20 or so amino acids I have that many patterns. Can anyone help me find an elegant way of doing this?
I know I could use string1.equals(string2) etc but I would really rather use regex for it. Any help would be much appreciated!
回答1:
You're passing Pattern.compile()
a boolean value, where it should be a string:
Pattern A = Pattern.compile("(gct)|(gcc)|(gca)|(gcg)");
回答2:
This:
/("gct")||("gcc")||("gca")||("gcg")/
Equals to :
/("gtc")/
Because double || means match nothing. And guess what? It will always match!
Instead try to use one |
/("gct")|("gcc")|("gca")|("gcg")/
Or even better:
"gc[tcag]"
Edit:
Wow didn't notice the boolean :) +1 to @Tim
来源:https://stackoverflow.com/questions/8437180/matching-multiple-strings-to-one-long-string-using-patterns