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?
- What is the logic of the split? I dont get it.PaNaVTEC– PaNaVTEC2013-02-13 11:40:51 +00:00Commented Feb 13, 2013 at 11:40
- Can you formulate rules on which these matches should be based? It helps with constructing a regexBergi– Bergi2013-02-13 11:41:46 +00:00Commented Feb 13, 2013 at 11:41
- i have edited my question have a lookDeveloper– Developer2013-02-13 11:43:10 +00:00Commented 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?Mark Reed– Mark Reed2013-02-13 11:45:38 +00:00Commented Feb 13, 2013 at 11:45
Add a comment |
2 Answers
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"