0

I have a problem with RegEx in java;

my line is :

CREATE CHAN:NAME=BTSM:1/BTS:2/TRX:5/CHAN:7,CHTYPE=TCHF_HLF,FHSYID=FHSY_0

and I want this :

content [0] = BTSM:1/BTS:2/TRX:5/CHAN:7
content [1] = CHTYPE
content [2] = TCHF_HLF
content [3] = FHSYID
content [4] = FHSY_0

I wrote this :

String[] content = value.split("^=/:|,|=|,$");

but it's not work :( so kindly inform me about that... Thanks a lot ...

2 Answers 2

4
String[] content = value.replaceFirst("^[^=]*=", "").split("[,=]");

should do what you want.

I don't understand how you derived "^=/:|,|=|,$" so I can't tell you where you went wrong, but here's a breakdown of what it does.

^=/:

This is going to skip the string =/: if it occurs at the beginning and stick an empty string at the start of the results. Perhaps you wanted a character set. [=/:] is a character set that matches any occurence of one of those characters.

,

This will split on any comma.

=

This will split on any equals sign.

,$

This will skip a comma at the end of the input (or just before a newline at the end of input) and if skipped will stick an empty string on the end of the split result.

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

1 Comment

+1 Simple, I like it. RegEx isn't the tool to solve all problems.
2

I don't know what the Hell that thing you're passing to split() is, but what you need to do is to split on any occurence of , or = after removing everything up through the first =. This can be accomplished with:

String[] content = (value.substring(value.indexOf('=') + 1)).split("[,=]");

1 Comment

Ooh, even better. Simple always makes me happy =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.