Reputation: 2821
I have a string row with code (2 chars) and name separated by >.
eg. CP >RENATO DE SA
, CP >FRAIS
. I want to split this row in pairs with code and name.
I have this text:
CT >RUSSO CT >JOSE AQUINO CP >RENATO DE SA CP >FRAIS CF >TAMARA STUCCHI CF >VANESSA JULKOWS CM >CRISTINA LOUSTA CM >HANS KROESCHEL CM >CONCEICAO MACIE CM >AIMEE FRARI CM >JONNY MOREIRA
Desired result:
CT, RUSSO
CT, JOSE AQUINO
CP, RENATO DE SA
CP, FRAIS
CF, TAMARA STUCCHI
CF, VANESSA JULKOWS
CM, CRISTINA LOUSTA
CM, HANS KROESCHEL
CM, CONCEICAO MACIE
CM, AIMEE FRARI
CM, JONNY MOREIRA
Upvotes: 1
Views: 2424
Reputation: 158
string.split("[÷+[-]]".toRegex()) look here i needed to split string with + ,div , - you just need to convert the string to regex As - is special symbol in regex we need to wrap it
hope in the same way you will be able to solve your problem too
Upvotes: 0
Reputation: 59950
You can split with this regex ( (?=[A-Z]{2} >)| >)
import java.util.*
fun main(args: Array<String>) {
val input = "CT >RUSSO CT >JOSE AQUINO CP >RENATO DE SA CP >FRAIS ...";
val split = input.split("( (?=[A-Z]{2} >)| >)".toRegex())
for (i in split.indices step 2)
println(split[i] + ", " + split[i + 1])
}
Outputs
CT, RUSSO
CT, JOSE AQUINO
CP, RENATO DE SA
CP, FRAIS
CF, TAMARA STUCCHI
CF, VANESSA JULKOWS
CM, CRISTINA LOUSTA
CM, HANS KROESCHEL
CM, CONCEICAO MACIE
CM, AIMEE FRARI
CM, JONNY MOREIRA
You can check the ideone demo
The regex will match two things ( (?=[A-Z]{2} >)| >)
(?=[A-Z]{2} >
space followed by two upper letters then a space then a >
sign, but we need the two Upper letters for that we use ?=
a positive lookahead|
or >
a space followed by >
signYou can check the regex demo here
Upvotes: 3
Reputation: 10562
You can do without regex:
replace(" >", ", ").replace(" ","\\n");
or (using regex)
replaceAll("\\s>", ", ").replaceAll("\\s","\\n");
Upvotes: 1