Distributed numbers and groups of letters without spaces

If I have a string like "11E12C108N", which is a concatenation of letter groups and number groups, how can I separate them without a space character between them?

For example, I want the resulting split to be:

tokens[0] = "11" tokens[1] = "E" tokens[2] = "12" tokens[3] = "C" tokens[4] = "108" tokens[5] = "N" 

I have it right now.

 public static void main(String[] args) { String stringToSplit = "11E12C108N"; Pattern pattern = Pattern.compile("\\d+\\D+"); Matcher matcher = pattern.matcher(stringToSplit); while (matcher.find()) { System.out.println(matcher.group()); } } 

What gives me:

 11E 12C 108N 

Can I make the original regex to completely split at a time? Instead of running regex on intermediate tokens again?

+6
source share
2 answers

Use the following regex and get a list of all matches. This will be what you are looking for.

 \d+|\D+ 

In Java, I think the code would look something like this:

 Matcher matcher = Pattern.compile("\\d+|\\D+").matcher(theString); while (matcher.find()) { // append matcher.group() to your list } 
+5
source

You can also use "search around" in regex mode

 String stringToSplit = "11E12C108N"; String[] tokens = stringToSplit .split("(?<=\\d)(?=\\D)|(?=\\d)(?<=\\D)"); System.out.println(Arrays.toString(tokens)); 

from [11, E, 12, C, 108, N]

The idea is to split between numbers ( \d ) and non-characters ( \d ). In other words, this is the place (empty line) that have:

  • before (?<=\d) and without digits after (?=\D)
  • not the number before (?<=\d) and the number after it (?=\D)

More information about (?<=..) and (?=..) (and a few more) can be found at http://www.regular-expressions.info/lookaround.html

+3
source

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


All Articles