user3003233
user3003233

Reputation: 31

Regex in Java that can split special characters and alphanumerics

I was researching on using regex in Java and found something interesting on this link. It says

[:punct:]   Punctuation symbols . , " ' ? ! ; : # $ % & ( ) * + - / < > = @ [ ] \ ^ _ { } | ~

are used to break down special characters. Is there anything similar in Java, let say i have a variable:

String sample = "I have $100";

is there anyway i can break the variable: I have $100 as

I
have
$
100

Upvotes: 0

Views: 2043

Answers (4)

Masudul
Masudul

Reputation: 21981

Try, look-around regex,

  String sample = "£9999";
  String[] arr = sample.split("(?<=[$£])|(?= )");
  for (String string : arr) {
     System.out.println(string);
  }

Output:

£
9999

Upvotes: 0

melc
melc

Reputation: 11671

You could get the output you are asking with the following java code,

 Pattern pattern = Pattern.compile("(\\$)|(\\w+)");/*(\\w*)"); changed to \\w+ to avoid empty matches, based on AlanMoore's remark*/
            Matcher matcher = pattern.matcher("I have $100");
            while(matcher.find()){
//                if(matcher.group().isEmpty())continue;

                System.out.println(matcher.group());
            }

Upvotes: 1

Roland Illig
Roland Illig

Reputation: 41686

A very simple regular expression for this task is \b, which matches a word boundary. After splitting you need to trim the results and filter out empty strings, then you get what you want.

Upvotes: 0

Pshemo
Pshemo

Reputation: 124275

Since you want to split only on £ or $ I would suggest to put them in your own character class [£$] instead using predefined one which contains many characters that probably shouldn't be split. So try something like split("\\s|(?<=[£$])") which will split on

  • \\s - every whitespace
  • (?<=[£$]) - every place that has £ or $ before it like $|100 (| represents such place). Mechanism used here is called look-behind.

Demo

for (String s : "I have $100 and £200".split("\\s|(?<=[£$])"))
    System.out.println(">" + s);

output:

>I
>have
>$
>100
>and
>£
>200

Upvotes: 2

Related Questions