简体   繁体   中英

Java - Check if a string only contains certain characters (i.e. DNA/RNA)

I'm struggling with regex.

I want to make something like this:

if (sequence.matches(A|T|G|C)){
String type = "DNA"
}
elseif (sequence.matches(A|U|G|C)){
String type = "RNA"
}

so that the type is only set to DNA if the sequence is only A,T,G or C but RNA if it is A,U,G or C

Regardless of the programming language, the regular expression you want should test that the string contains only the characters of interest from start to finish:

^[ACGT]+$

^ means "start of string". [ACGT] indicates one of those 4 letters. + indicates that there must be one or more of those characters. $ means "end of string".

So this means that your string must have nothing in it but A, C, G, or T, and there must be at least one of those.

Regex may not be your most efficient option:

static boolean consistsOf(String s, String of) {
  for ( int i = 0; i < s.length(); i++ ) {
    if ( of.indexOf(s.charAt(i)) == -1 ) {
      return false;
    }
  }
  return true;
}

You can use the below regex

if (sequence.matches("[ATGC]+")) { // + for one or more occurrences, * for zero or more occurrences

and the same for the other check as well.

else if (sequence.matches("[AUGC]+")) { // + for one or more occurrences, * for zero or more occurrences

Also, you need to specify the String within doubles quotes if(str.matches("strInDoubleQuotes")) .

A normal expression would be: "[ATGC]+" which matches with A , T , G or C . The expression [ATGC] is known as Character class to which the input string should match. And an expression X+ is part of the Quantifiers which says that the expression X occurs one or more times.

"ATCCGT".matches("[ATGC]+")

Set theory would dictate this simplification:

String type = (sequence.contains("U")) ? "RNA" : "DNA";    
String type = (sequence.contains("T")) ? "DNA" : "RNA";

No? Frankly not even sure you need 2 expressions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM