4

I have a String that I want to split;

String x = "abc4.5efg2hij89k.9";

I want the output as

abc, 4.5, efg, 2, hij, 89, k, .9

I can easily split across digits and non digits however '.' is considered a character.

x.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)")
[abc, 4, ., 5, efg, 2, hij, 89, k., 9]

What is the best way of supporting doubles?

1
  • Not sure how in java but in other lang you can make your own groups with brackets like [A-Z,a-z] to exclude . from matching Commented Dec 19, 2014 at 1:41

3 Answers 3

5

Have you considered letter characters instead of using the \D token?

String s = "abc4.5efg2hij89k.9";
String[] parts = s.split("(?<=[a-z])(?=[\\d.])|(?<=\\d)(?=[a-z])");
System.out.println(Arrays.toString(parts));

Output

[abc, 4.5, efg, 2, hij, 89, k, .9]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! to match uppercase as well String[] parts = s.split("(?<=[A-Za-z])(?=[\\d.])|(?<=\\d)(?=[A-Za-z])");
1

You could do matching instead of splitting and then store the matches to an array list.

[^\\d.]+|\\d*(?:\\.\\d+)?

DEMO

String x = "abc4.5efg2hij89k.9";
Pattern regex = Pattern.compile("[^\\d.]+|\\d*(?:\\.\\d+)?");
Matcher matcher = regex.matcher(x);
ArrayList<String> returnValue= new ArrayList<String>();
while(matcher.find())
     {
         if(matcher.group().length() != 0)
         {
             returnValue.add(matcher.group());
         }
     }
System.out.println(returnValue);

Output:

[abc, 4.5, efg, 2, hij, 89, k, .9]

Comments

0
(?<=[a-zA-Z])(?=[^a-zA-Z])|(?<=[^a-zA-Z])(?=[a-zA-Z])

Split by this.See demo.

https://regex101.com/r/vN3sH3/19

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.