Search for punctuation marks in a string, and then search for their index for a substring

I have a line where I need to extract a substring from it, based either on the first introduction of the punctuation mark, or on the first occurrence of a digit. for instance

from Taltz 80mg autoinjectorI need to extract Taltzor from Trulicity 0.75mg, weeklyI need to extractTrulicity

Here is my code:

 char [] punctuations = {'.' , ',' , ';' , ':','"' , '\'' ,'/', ')' , '('};

 String value = "Taltz, 80mg autoinjector";
 int pos = value.replaceFirst("^(\\D+).*$", "$1").length();     

                for(int j = 0; j < value.length(); j++) {
                    for (int k = 0; k < punctuations.length;k++){
                        if(value.charAt(j) == punctuations[k]){
                            value = value.substring(0,value.indexOf(punctuations[k]));
                            break;
                        }
                    }
                }

            if(value.matches(".*\\d+.*")){
                value = value.substring(0, pos);
            }

            System.out.println(value);


        }

Is there a more efficient way to do this?

+4
source share
1 answer

You can define the part you want to keep and capture it with a regex:

String s = "Taltz test 80mg autoinjector";
Pattern pattern = Pattern.compile("([a-zA-Z ]+).*");
Matcher matcher = pattern.matcher(s);

if(matcher.matches()) {
  System.out.println("matches : " + matcher.group(1).trim());
} else {
  System.out.println("Does not match");
}

Output :

Taltz test

You can also capture anything that is "neither a punctuation mark, nor a number" with the following regular expression:

Pattern pattern = Pattern.compile("([^0-9;,:.?]+).*");

(same conclusion)

+1

Source: https://habr.com/ru/post/1678118/


All Articles