Matching multiple strings to one long string using patterns

隐身守侯 提交于 2020-01-04 06:51:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!