-1

Sorry for the bad title, but really could not find some other words for it. But, I am working in java and i have the following pattern matching to do.

The pattern is (\\w)*(\\s+)(\\w)*(\\,)(\\s*)(\\w)*(\\,)?(\\s*)(\\w)*

The String to be matched is of the type "add r0, r1, r2". Now, how can I extract the all the individual strings from the above string, ie. add,r0,r1 and r2? To make it clearer, if the input string were "mov r1, r4", I would like to extract mov, r1 and r4. How to go about this?

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
alpha42
  • 55
  • 6

2 Answers2

0

Hint: Don't try to match the full string using a complex regex and use String#split(regex) to split it using space or comma i.e. "[, ]" and get your tokens from resulting array.

String input = "add r0, r1, r2";
String[] arr = input.split("[ ,]+"));
// [add, r0, r1, r2]
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

You can try this:-

String yourinput="add r0, r1, r2"
String[] s=yourinput.split("[\\s,]+");
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331