我需要计算输入链中指定模式的出现次数,并为每个模式生成一个报告。 输入字符串将包含1 AA AATTCGAA结尾 1表示要搜索的一个模式,AA表示模式,下一个是您搜索AA的部分。
My idea is to :
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("How many patterns do you want and enter patterns and DNA Sequence(type 'end' to signify end):");
String DNA = s.nextLine();
process(DNA);
}
public static void process(String DNA){
String number = DNA.replaceFirst(".*?(\\d+).*", "$1");
int N = Integer.parseInt(number);
DNA.toUpperCase();
String[] DNAarray;
DNAarray = DNA.split(" ");
for(int i=1; i<N; i++){
int count=0;
for(int j =0; j < DNAarray.length; j++) {
if(DNAarray[i+N].contains(DNAarray[i])){
count= count++;
}
}
System.out.println("Pattern:"+DNAarray[i]+ "Count:"+count);
}
答案 0 :(得分:0)
这应该这样做:
using System;
using System.Text.RegularExpressions;
public class Program
{
public void Main()
{
Console.WriteLine(PatternCount("1 AA AADDRRSSAA"));
}
public int PatternCount(string sDNA) {
Regex reParts = new Regex("(\\d+)\\s(\\w\\w)\\s(\\w+)");
Match m = reParts.Match(sDNA);
if (m.Success)
{
return Regex.Matches(m.Groups[3].Value, m.Groups[2].Value).Count;
}
else
return 0;
}
}
第一个RE将输入拆分为计数,模式和数据。 (不确定为什么要限制要搜索的模式数量。此代码忽略了这一点。根据需要修改...) 第二个RE等于所需的模式,“匹配”计算出现的次数。从这里开始工作。
此致
(今天我感觉很好,做人们的工作;))
答案 1 :(得分:0)
真的没必要把搜索次数。而且,实际上这可以完成
一个正则表达式。我不记得Dot-Net是否支持\G
锚,
但无论如何这根本不是必要的。我把它留在了。
每场比赛:
找到一把新钥匙
最后捕获键子串匹配
只需按键即可提升搜索位置。
所以,坐在Find循环中
在每个匹配上打印'Key'捕获缓冲区,
然后打印捕获集合“值”计数。
这就是它的全部内容。
正则表达式将搜索重叠键。要将其更改为专用键,
如评论中所示,将=
更改为:
。
你也可以使它更具体一点。例如,将所有\w
更改为[A-Z]
等等......
正则表达式:
(?:
^ [ \d]*
| \G
)
(?<Key> \w+ ) #_(1)
[ ]+
(?=
(?: \w+ [ ]+ )*
(?= \w )
(?:
(?= # <- Change the = to : to get non-overlapped matches
(?<Values> \1 ) #_(2)
)
| .
)*
$
)
这是一个perl测试用例
# $str = '2 6 AA TT PP AAATTCGAA';
# $count = 0;
#
# while ( $str =~ /(?:^[ \d]*|\G)(\w+)[ ]+(?=(?:\w+[ ]+)*(?=\w)(?:(?=(\1)(?{ $count++ }))|.)*$)/g )
# {
# print "search = '$1'\n";
# print "found = '$count'\n";
# $count = 0;
#
# }
#
# Output >>
#
# search = 'AA'
# found = '3'
# search = 'TT'
# found = '1'
# search = 'PP'
# found = '0'
#
#