使用模式将多个字符串匹配为一个长字符串 [英] Matching multiple strings to one long string using patterns

查看:75
本文介绍了使用模式将多个字符串匹配为一个长字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一行DNA代码,我正在尝试使用Java正则表达式将密码子(3个字母序列)与氨基酸匹配.下面是其中一种模式的示例:

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"));

使用或不用圆括号,此语法似乎不起作用.最终,该代码的目的是计算在DNA字符串中发现氨基酸的次数,并且由于存在20个左右的氨基酸,因此我具有许多模式.有人可以帮我找到一种优雅的方式吗?

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?

我知道我可以使用string1.equals(string2)等,但是我真的更喜欢使用正则表达式.任何帮助将不胜感激!

I know I could use string1.equals(string2) etc but I would really rather use regex for it. Any help would be much appreciated!

推荐答案

您正在传递Pattern.compile()一个布尔值,该值应该是字符串:

You're passing Pattern.compile() a boolean value, where it should be a string:

Pattern A = Pattern.compile("(gct)|(gcc)|(gca)|(gcg)");

这篇关于使用模式将多个字符串匹配为一个长字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆