1

I want to take a string according to my regex in java. Suppose i have a String "R12T12W5P12T5L3"
. And now i want to have something like this : myStr[0]="R12T12",myStr[1]="W5P12",myStr[2]=T5L3. I want to have my regex first a character then a number then again a character and last a number. How can i do that?

4
  • What is the logic of the split? I dont get it. Commented Feb 13, 2013 at 11:40
  • Can you formulate rules on which these matches should be based? It helps with constructing a regex Commented Feb 13, 2013 at 11:41
  • i have edited my question have a look Commented Feb 13, 2013 at 11:43
  • Numbers are characters, so I assume you mean you want every sequence of "letter, digits, letter, digits" to be a separate item after the split. What have you tried? Commented Feb 13, 2013 at 11:45

2 Answers 2

3
String s="R12T12W5P12T5L3"; String regex = "([A-Z]\\d+){2}"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(s); while(m.find()){ System.out.println(m.group(0)); } 

this will print

R12T12 W5P12 T5L3 

you can put them into a list and convert into array at the end.

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

Comments

0

All operations from the regex to the string building, in javascript :

var str = "R12T12W5P12T5L3"; var result = str.split(/(?=[^\d]){2}/).map(function(v,i,a){ return i%2 ? a[i-1]+v+'",' : 'myStr['+(i/2)+']="' }).join('').slice(0,-1); 

Result :

myStr[0]="R12T12",myStr[1]="W5P12",myStr[2]="T5L3" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.