N
newsnet customer
Hi,
Would like to use regular expression to find patterns in a string.
Consider a string seq "ABCDEFHIJKLMNOP";
Would like to find three patterns (ABC, NOP, HIJ) which ever comes first.
Expect the output "0" representing the index of ABC.
Long story short, couldn't get it to work and tried the two attempts below:
Much help appreciated on this regular expression, which is doing my head in.
Cheers
ST
Attempt One:
String seq = "ABCDEFHIJKLMNOP";
Pattern p1 = Pattern.compile("ABC" || "NOP" || "HIJ");
Matcher m = p1.matcher(seq);
while ( m.find())
{
System.out.println(m.start());
//nothing happens
}
Attempt Two:
String seq = "ABCDEFHIJKLMNOP";
Pattern p1 = Pattern.compile("ABC");
Pattern p2 = Pattern.compile("NOP");
Pattern p3 = Pattern.compile("HIJ");
Matcher m1 = p1.matcher(seq);
Matcher m2 = p2.matcher(seq);
Matcher m3 = p3.matcher(seq);
while ( m1.find() || m2.find() || m3.find() )
{
//won't work cos I don't know which matcher gave me the true
}
Would like to use regular expression to find patterns in a string.
Consider a string seq "ABCDEFHIJKLMNOP";
Would like to find three patterns (ABC, NOP, HIJ) which ever comes first.
Expect the output "0" representing the index of ABC.
Long story short, couldn't get it to work and tried the two attempts below:
Much help appreciated on this regular expression, which is doing my head in.
Cheers
ST
Attempt One:
String seq = "ABCDEFHIJKLMNOP";
Pattern p1 = Pattern.compile("ABC" || "NOP" || "HIJ");
Matcher m = p1.matcher(seq);
while ( m.find())
{
System.out.println(m.start());
//nothing happens
}
Attempt Two:
String seq = "ABCDEFHIJKLMNOP";
Pattern p1 = Pattern.compile("ABC");
Pattern p2 = Pattern.compile("NOP");
Pattern p3 = Pattern.compile("HIJ");
Matcher m1 = p1.matcher(seq);
Matcher m2 = p2.matcher(seq);
Matcher m3 = p3.matcher(seq);
while ( m1.find() || m2.find() || m3.find() )
{
//won't work cos I don't know which matcher gave me the true
}