How to extract uppercase substrings from a string in Java?

I need a piece of code with which I can extract substrings that are in uppercase from a string in Java. For instance:

"a:[AAAA|0.1;BBBBBBB|-1.90824;CC|0.0]" 

I need to extract CC BBBBBBB and AAAA

+4
source share
5 answers

You can do this with String[] split(String regex) . The only problem may be with empty lines, but they are easy to filter out:

 String str = "a:[AAAA|0.1;BBBBBBB|-1.90824;CC|0.0]"; String[] substrings = str.split("[^AZ]+"); for (String s : substrings) { if (!s.isEmpty()) { System.out.println(s); } } 

Output:

 AAAA BBBBBBB CC 
+6
source

This should demonstrate the correct syntax and method. More information can be found here http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html and http://docs.oracle.com/javase/1.5. 0 / docs / api / java / util / regex / Matcher.html

 String myStr = "a:[AAAA|0.1;BBBBBBB|-1.90824;CC|0.0]"; Pattern upperCase = Pattern.compile("[AZ]+"); Matcher matcher = upperCase.matcher(myStr); List<String> results = new ArrayList<String>(); while (matcher.find()) { results.add(matcher.group()); } for (String s : results) { System.out.println(s); } 

The [AZ]+ is a regular expression that does most of the work. There are many powerful regular expression tutorials if you want to work more with it.

+4
source

If you want to simply extract the entire uppercase letter, use [AZ]+ if you want only an uppercase substring, which means that if you do not need lowercase letters ( HELLO normal, but HELLO not), then use \b[AZ]+\b

0
source

I think you need to replace all regular expressions in order to turn the character you don't want into a delimiter, maybe something like this:

  • str.replaceAll ("[^ AZ] +", "")
  • Trim any leading or trailing spaces.
  • Then, if you want, you can call str.split ("")
0
source

This is probably what you are looking for:

 import java.util.regex.Pattern; import java.util.regex.Matcher; public class MatcherDemo { private static final String REGEX = "[AZ]+"; private static final String INPUT = "a:[AAAA|0.1;BBBBBBB|-1.90824;CC|0.0]"; public static void main(String[] args) { Pattern p = Pattern.compile(REGEX); // get a matcher object Matcher m = p.matcher(INPUT); List<String> sequences = new Vector<String>(); while(m.find()) { sequences.add(INPUT.substring(m.start(), m.end())); } } } 
0
source

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


All Articles