1

How can I put the "no", "no", "0.002", "0.998" below into the String array with regex in Java?

 String lines = "     1       2:no       2:no       0.002,*0.998"

Could someone please tell me how to write "theRegex" below?

String[] matches = lines.split(theRegex); // => ["no", "no", "0.002", "0.998"]

In Python, it will be just one line:

matches = line =~ /\d:(\w+).*\d:(\w+).*\((\w+)\)/

But how about Java?

1
  • 2
    What are the rules for splitting exactly? Commented Mar 19, 2013 at 14:40

3 Answers 3

2

theRegex="[\\s,*]+" (one or more spaces, commas or asterisk)

Input 1 2:no 2:no 0.002,*0.998 Output ["1","2:no","2:no","0.002","0.9"]

Edit

The input String is " 1 2:no 2:no 0.002,*0.998", and the expected output is ["no", "no", "0.002", "0.998"]

In that case it is not possible to use split alone, because for ignoring 1 you need to treat \d as a delimiter, but \d is also part of the data in 0.002.

What you can do is:

   Pattern pattern = Pattern.compile("^\\d(?:$|:)");
   String[] matches = lines.trim().split("[\\s,*]+");
   List<String> output = new LinkedList<String>(Arrays.asList(matches));
   for (Iterator<String> it=output.iterator(); it.hasNext();) {
     if (Pattern.matcher(it.next()).find()) it.remove();
   }

find("^\\d(?:$|:") matches strings of the form digit or digit:whatever. Note that the pattern is compiled once, then it is applied to the strings in the list. For each string one has to construct a matcher.

Sign up to request clarification or add additional context in comments.

4 Comments

The it will become ["", "1", "2:no", "2:no", "0.002,*0.998"] but not ["no", "no", "0.002", "0.998"]. Also, something doesn't match above pattern will also get split but the space and being put into the array...
1. Do lines.trim().split(...) if lines may begin with spaces. 2. Can you provide the counterexample and the expected output in that case?
The input String is " 1 2:no 2:no 0.002,*0.998", and the expected output is ["no", "no", "0.002", "0.998"] which is an array stores all this 4 elements. Thanks!!
Thanks Javier, but the String type iterator doesn't have a function call "find"... would you please advise how can I use the find over there? Thanks.
1

Try this regex...

(^\d|[\d]:|[\\s, *]+)+

Comments

0
String s = "1       2:no       2:no       0.002,*0.998";
String[] arr = s.split(" +");

4 Comments

The it will become ["", "1", "2:no", "2:no", "0.002,*0.998"] but not ["no", "no", "0.002", "0.998"]. Also, something doesn't match above pattern will also get split but the space and being put into the array...
@Reimeus yes but before viewing expected answer.
@Kevin What is your condition for regex?
@AchintyaJha, I don't quite understand what do you mean condition... The input String is " 1 2:no 2:no 0.002,*0.998", and the expected output is ["no", "no", "0.002", "0.998"] which is an array stores all this 4 elements. Thanks!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.