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));
}
Related
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("\":\\{");
I have a string say, "1.0+20*30.2-4.0/10.1" which I want to split in such a way that I will have a string array say
strArr = {"1.0", "20", "30.2", "4.0",10.1}
I wrote the following code for this
public class StringSplitDemo {
public static void main(String[] args) {
String str = "1.0+20*30.2-4.0/10.1";
String[] strArr = (str.split("[\\+-/\\*]"));
for(int i=0;i<strArr.length;i++)
System.out.print(strArr[i]+" ");
}
}
Rather than printing the expected output(by me) i.e 1.0 20 30.2 4.0 10.1 it prints
output: 1 0 20 30 2 4 0 10 1
which seems to split the string also around "." even if I didn't include it in the regex pattern.
What I'm missing here?
What is the right way to do this?
Use
String str = "1.0+20*30.2-4.0/10.1";
String[] strArr = str.split("[-+/*]");
System.out.print(Arrays.toString(strArr));
See the online Java demo
The [\\+-/\\*] character class matches more than just the 4 chars you defined, as - created a range between + and /.
You could fix the regex by escaping the hyphen, but the pattern looks much cleaner when you put the - at the start (or end) of the character class, where you do not have to escape it as there, the hyphen is treated as a literal -.
The issue was in regex
So you need to escape + otherwise it will treat it as atleast once
String[] strArr = (str.split("[\\-/\\*\\+]"));
By the way escape symbol here is not required. It can simply be written as
String[] strArr = (str.split("[-/*+]"));
I need help making a delimiter for multiple characters
I need a String delimiter for
these characters
( ) " ; : , ? ! .
I've tried:
private String delimiter = "()\":;,?!.";
private String delimiter = "[()\":;,?!.]";
private String delimiter = "\\(\\)\"\\:\\;\\,\\?\\!\\.";
Seems I can only make them work one at a time..
Any insight is greatly appreciated.
If it matters this is how its going into array:
foo = line.split(delim);
If you want to split on any of those characters, you can separate each one with an alternation: |. Otherwise, the string will only be split when all of those characters are present.
String delimiter = "\\(|\\)|\"|\\:|\\;|\\,|\\?|\\!|\\.";
Also, you're unnecessarily escaping a few characters, this would also work:
String delimiter = "\\(|\\)|\"|:|;|,|\\?|!|\\.";
Almost there with nr. 3
#Test
public void delim() {
String delimiter = "[\\(\\)\"\\:\\;\\,\\?\\!\\.]";
String[] split = "Hello(World)How:are;You;doing,today?You!sir.I mean"
.split(delimiter);
System.out.println(Arrays.toString(split));
}
Output
[Hello, World, How, are, You, doing, today, You, sir, I mean]
You missed the square brackets.
To avoid all the quoting you may use Pattern#quote
String delimiter = "[" + Pattern.quote("()\":;,?!.") + "]";
Returns a literal pattern String for the specified String.
This method produces a String that can be used to create a Pattern that would match the string s as if it were a literal pattern.
Metacharacters or escape sequences in the input sequence will be given no special meaning.
| is required between:
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
I need to split a string base on delimiter - and .. Below are my desired output.
AA.BB-CC-DD.zip ->
AA
BB
CC
DD
zip
but my following code does not work.
private void getId(String pdfName){
String[]tokens = pdfName.split("-\\.");
}
I think you need to include the regex OR operator:
String[]tokens = pdfName.split("-|\\.");
What you have will match:
[DASH followed by DOT together] -.
not
[DASH or DOT any of them] - or .
Try this regex "[-.]+". The + after treats consecutive delimiter chars as one. Remove plus if you do not want this.
You can use the regex "\W".This matches any non-word character.The required line would be:
String[] tokens=pdfName.split("\\W");
The string you give split is the string form of a regular expression, so:
private void getId(String pdfName){
String[]tokens = pdfName.split("[\\-.]");
}
That means to split on any character in the [] (we have to escape - with a backslash because it's special inside []; and of course we have to escape the backslash because this is a string). (Conversely, . is normally special but isn't special inside [].)
Using Guava you could do this:
Iterable<String> tokens = Splitter.on(CharMatcher.anyOf("-.")).split(pdfName);
For two char sequence as delimeters "AND" and "OR" this should be worked. Don't forget to trim while using.
String text ="ISTANBUL AND NEW YORK AND PARIS OR TOKYO AND MOSCOW";
String[] cities = text.split("AND|OR");
Result : cities = {"ISTANBUL ", " NEW YORK ", " PARIS ", " TOKYO ", " MOSCOW"}
pdfName.split("[.-]+");
[.-] -> any one of the . or - can be used as delimiter
+ sign signifies that if the aforementioned delimiters occur consecutively we should treat it as one.
I'd use Apache Commons:
import org.apache.commons.lang3.StringUtils;
private void getId(String pdfName){
String[] tokens = StringUtils.split(pdfName, "-.");
}
It'll split on any of the specified separators, as opposed to StringUtils.splitByWholeSeparator(str, separator) which uses the complete string as a separator
String[] token=s.split("[.-]");
It's better to use something like this:
s.split("[\\s\\-\\.\\'\\?\\,\\_\\#]+");
Have added a few other characters as sample. This is the safest way to use, because the way . and ' is treated.
Try this code:
var string = 'AA.BB-CC-DD.zip';
array = string.split(/[,.]/);
You may also specified regular expression as argument in split() method ..see below example....
private void getId(String pdfName){
String[]tokens = pdfName.split("-|\\.");
}
s.trim().split("[\\W]+")
should work.
you can try this way as split accepts varargs so we can pass multiple parameters as delimeters
String[]tokens = pdfName.split("-",".");
you can pass as many parameters that you want.
If you know the sting will always be in the same format, first split the string based on . and store the string at the first index in a variable. Then split the string in the second index based on - and store indexes 0, 1 and 2. Finally, split index 2 of the previous array based on . and you should have obtained all of the relevant fields.
Refer to the following snippet:
String[] tmp = pdfName.split(".");
String val1 = tmp[0];
tmp = tmp[1].split("-");
String val2 = tmp[0];
...