Regex split by ":{ in java? - java

basically I have:
String str = "Stream: {"stream":null,"_links":{"self":"https://api.twitch.tv/kraken/streams/tfue","channel":"https://api.twitch.tv/kraken/channels/tfue"}}";
I want to split the str by ":{
but when I do:
String[] BuftoStringparts = BuftoString.split("\":{");
I get below exception:
java.util.regex.PatternSyntaxException: Illegal repetition near index 1
":{
^
All replies are much appreciated :)

The main reason this happens:
java.util.regex.PatternSyntaxException: Illegal repetition near index 1 ":{ ^
It's because they are special characters in Java regular expressions so you need to use it escaped for the regex, so by following way:
String[] BuftoStringparts = BuftoString.split("\":\\{");

First of all you need to escape " in your JSON String, so the resulting String will be:
String str = "Stream: {\"stream\":null,\"_links\":{\"self\":\"https://api.twitch.tv/kraken/streams/tfue\",\"channel\":\"https://api.twitch.tv/kraken/channels/tfue\"}}";
Now as mentioned by others, you also need to escape regex special characters in your regex.
You can try your split by following regex:
String[] BuftoStringparts = BuftoString.split("\":\\{");

Related

Scala Pattern Syntax Exception

I'm trying to split a string in by the characters "}{". However I am getting an error:
> val string = "{one}{two}".split("}{")
java.util.regex.PatternSyntaxException: Illegal repetition near index 0
}{
^
I am not trying to use regex or anything. I tried using "\}\{" and it also doesn't work.
Well... the reason is that split treats its parameter string as a regular expression.
Now, both { and } are special character in regular expressions.
So you will have to skip the special characters of regex world for split's argument, like this,
val string = "{one}{two}".split("\\}\\{")
// string: Array[String] = Array({one, two})
Escape the {
val string = "{one}{two}".split("}\\{")
There are two ways to force a metacharacter to be treated as an ordinary character:
-> precede the metacharacter with a backslash.
String[] ss1 = "{one}{two}".split("[}\\{]+");
System.out.println(Arrays.toString(ss1));
output:
[one, two]
-> enclose it within \Q (which starts the quote) and \E (which ends it).
When using this technique, the \Q and \E can be placed at any location within the expression, provided that the \Q comes first.
String[] ss2 = "{one}{two}".split("[}\\Q{\\E]+");
System.out.println(Arrays.toString(ss2));
output:
[one, two]

How to split String with special character using Java

What is the way, how to split String with special character using Java?
I have very simple captcha like this:
5 + 10
String captcha = "5 + 10";
String[] captchaSplit = captcha.split("+");
And I get error:
Exception in thread "main" java.util.regex.PatternSyntaxException:
Dangling meta character '+' near index 0
How to fix it?
Thank you.
+ is a reserved character in regular expression and split takes regExp as a parameter. You can escape it by \\+ which will now match +.
Type it in square brackets
String captcha = "5 + 10";
String[] captchaSplit = captcha.split("[+]");
If you need to split String with multiple special symbols/characters, it's more convenient to use Guava library that contains Splitter class:
#Test
public void testSplitter() {
String str = "1***2***3";
List<String> list = Lists.newArrayList(Splitter.on("***").split(str));
Assert.assertThat(list.size(), is(3));
}

String.split not working with combination of delimiter {^

I am trying to split the string with combination of {^
How to use combination of delimiter for splitting the string.
The sample data is :
String str = "0002{^000000000000001157{^000006206210015461{^PR{^ID{^62499{^";
The delimiter passed to String.split() is a regex. As { and ^ are characters with special meaning within a regex, you need to escape them if you want to use them as literals:
String[] tokens = str.split("\\{\\^");
split method in java takes an regex as an input.
so if you want to split the string using '{' and '^' then you need to do the following:
String str = "0002{^000000000000001157{^000006206210015461{^PR{^ID{^62499{^";
String[] splitted = str.split("\\{\\^"); //note \\ before { and ^
You have to escape { and ^ in your split Statement, because both are Special character in regex:
s.split("\\{\\^");

split string based on comma delimiter

what is wrong in the following code?
String selectedCountriesStr = countries.replaceAll("[", "").replaceAll("]", "").trim();
String[] selectedCountriesArr = selectedCountriesStr.split(",");
Input String [10000,20000,304050,766666]
Getting error java.util.regex.PatternSyntaxException: Unclosed character class near index 0
You have to escape square brackets because replaceAll() interprets the first argument as a regular expression:
replaceAll("\\[", "")
^^
because, as the error message tells you, the are used for character classes in a regex. Double backslashes are necessary, because "\[" would be an invalid escape sequence. Since the backslash is escaped, the regex engine only receives one backslash.
Also, you can use
replace("[", "")
it will also replace all occurrences of the given CharSequence as is.
You can read more about it in JavaDoc.
Brackets are regex metacharacters, you need to prefix them with a backslash:
.replaceAll("\\[", "").replaceAll("\\]", "")
Also, since this is a simple string substitution, you'd better use .replace():
.replace("[", "").replace("]", "")
String str = "hi,hello,abc,example,problems";
String[] splits = str.split(",");
System.out.println("splits.size: " + splits.length);
for(String asset: splits){
System.out.println(asset);
}
Split function will easily split your string like this

How to split the string using '^' this special character in java?

I want to split the following string "Good^Evening" i used split option it is not split the value. please help me.
This is what I've been trying:
String Val = "Good^Evening";
String[] valArray = Val.Split("^");
I'm assuming you did something like:
String[] parts = str.split("^");
That doesn't work because the argument to split is actually a regular expression, where ^ has a special meaning. Try this instead:
String[] parts = str.split("\\^");
The \\ is really equivalent to a single \ (the first \ is required as a Java escape sequence in string literals). It is then a special character in regular expressions which means "use the next character literally, don't interpret its special meaning".
The regex you should use is "\^" which you write as "\\^" as a Java String literal; i.e.
String[] parts = "Good^Evening".split("\\^");
The regex needs a '\' escape because the caret character ('^') is a meta-character in the regex language. The 2nd '\' escape is needed because '\' is an escape in a String literal.
try this
String str = "Good^Evening";
String newStr = str.replaceAll("[^]+", "");

Categories