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
CC
BBBBBBB
AAAA
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[] split(String regex)
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
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.
[AZ]+
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
HELLO
\b[AZ]+\b
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:
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())); } } }
Source: https://habr.com/ru/post/1392955/More articles:Sending email using S / MIME (PHP) application - phpAndroid - onConfigurationChanged () is called before onResume () after pausing - androidCRC-32 implementation in java.util.zip.CRC32 - javaInvoking a virtual method from a base class method - c ++how to find out the name of a PDF file using the link to the CGPDFDictionaryRef object - iosLoops and stacks, assignment of homework palindrome homework - javaCustomize the iPhone app taskbar - iosBlackberry BrowserField2 Provides Excessively Slow Download Time - blackberryWhy did I get a different component size in the IDE? - constructorrails - Could not find rspec: install generator. - generatorAll Articles