I’m having a little trouble constructing the regular expression using java.
The constraint is, I need to split a string seperated by !. The two strings will be enclosed in double quotes.
For example:
"value"!"value"
If I performed a java split() on the string above, I want to get:
value
value
However the catch is value can be any characters/punctuations/numerical character/spaces/etc..
So here’s a more concrete example. Input:
""he! "l0"!"wor!"d1"
Java’s split() should return:
"he! "l0
wor!"d1
Any help is much appreciated. Thanks!
Try this expression:
(".*")\s*!\s*(".*")Although it would not work with split, it should work with
PatternandMatcherand return the 2 strings as groups.Edit:
This would not work for all cases, e.g.
"he"!"llo" ! "w" ! "orld"would get the wrong groups. In that case it would be really hard to determine which ! should be the separator. That’s why often rarely used characters are used to separate parts of a string, like@in email addresses 🙂