This question already has answers here:
How can I use "." as the delimiter with String.split() in java [duplicate]
(8 answers)
Closed 9 years ago.
I am trying to do a String.split on a website address using the "." so that I can find the domain name of the website.
However, when I do this:
String href = "www.google.com";
String split[] = href.split(".");
int splitLength = split.length;
It tells me that the splitLength variable is 0. Why is this, and how can I make this work?
Try using this to split the string:
href.split("\\.");
Explanation: split splits on a regex, not on a regular substring. In regexes, . is the metacharacter for 'match any character', which we don't want. So we have to escape it using a backslash \. But \ is also a metacharacter for escaping in Java strings, so we need to escape it twice.
Split uses a regex so do:
String split[] = href.split("\\.");
Related
This question already has answers here:
How do I express ":" but not preceded by "\" in a Java regular expression?
(2 answers)
Closed 4 years ago.
I need to split a string in below condition.
Split with / and should not split if it has \/.
Split with = and should not split if it has \=.
Basically looking for TWO regular expressions which split with above conditions and avoid if it has escape character.
You may try using lookarounds here:
String input = "Hello/World";
String[] parts = input.split("(?<!\\\\)[/=]");
The above single regex covers both splitting cases. It uses a negative lookbehind, which asserts that the character which immediately precedes the / or = is not a backslash.
You could use a negative lookbehind (?<!\\\\) to assert what is on the left is not a backslash.
Then match 1+ times a forward slash or an equals sign [/=]+ using a character class:
String regex = "(?<!\\\\)[/=]+";
Java demo | Regex demo
This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I have a file with content
1|yes|
2|yes|
3|yes|
4|yes|
5|yes|
6|yes|
7|yes|
8|yes|
9|yes|
10|yes|
11|yes|
12|yes|
13|yes|
14|yes|
15|yes|
I use java's String[] tokens = split("|"); to split each line, but it returns (for example splitting "10|yes|") [1,0,|,y,e,s,|]. It seems instead of splitting by "|", it splits every character. Anyone has any idea on it? Thanks!
split accepts a regular expression. | has a specific meaning in regular expressions, it expresses an alternation. To actually split on |, you have to escape it in the regex with a backslash. Since you specify the regex using a string literal, and backslashes are special in string literals, you have to escape that with another backslash:
String[] tokens = str.split("\\|");
In the general case, if you want to use the contents of a string literally, you can use Pattern.quote to automatically escape any special characters. You don't really need it here, but it's useful for end-user-entered values:
String[] tokens = str.split(Pattern.quote(stringToSplitOnLiterally));
This question already has answers here:
How to split a comma separated String while ignoring escaped commas?
(6 answers)
Closed 9 years ago.
I'm looking for a regular expression to match , but ignore \, in Java's regex engine. This comes close:
[^\\],
However, it matches the previous character (in addition to the comma), which won't work.
Perhaps the regular expression approach is the wrong one altogether. I was intending to use String.split() to parse a simple CSV file (can't use an external library) with escaped commas.
You need a negative look-behind assertion here:
String[] arr = str.split("(?<![^\\\\]\\\\),");
Note that you need 4 backslashes there. First escape the backslash for Java string literal. And then again escape both the backslashes for regex.
This question already has answers here:
How can I use "." as the delimiter with String.split() in java [duplicate]
(8 answers)
Closed 9 years ago.
I would like to identify 2 types of patterns in a string. They are:
1) xxx:xxxxxxx:xxx.xx
2) xxx.xxx.xxx.xx:xxxxxxxxxx
Basically I want to know how to identify a literal "." in a string.
Since . means any character, what should I type when I am looking for a literal "."?
You can either escape the . like this \\.
or
use it within character class like this [.]
Try using, String [] stringArray = string.split("\\.");
escaping the "."
And then int periods = stringArray.length;
Telling you how many periods there are in your "String"
Just a start on escaping characters. I am unsure what your question is asking so I just gave an intro to escaping.
good luck
String text ="xxx.xxx.xxx.xx:xxxxxxxxxx";
String patternString = "(.*)(\\.)(.*)";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);
boolean matches = matcher.matches();
System.out.println(matches);
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
java String.replaceAll without regex
I have a string and I need to replace some parts of it.
The replacement text contains regex wild chars though. Example:
String target = "Something * to do in ('AAA', 'BBB')";
String replacement = "Hello";
String originalText = "ABCDEFHGIJKLMN" + target + "ABCDEFHGIJKLMN";
System.out.println(originalText.replaceAll(target, replacement));
I get:
ABCDEFHGIJKLMNSomething * to do in ('AAA', 'BBB')ABCDEFHGIJKLMN
Why doesn't the replacement occur?
Because *, ( and ) are all meta-characters in regular expressions. Hence all of them need to be escaped. It looks like Java has a convenient method for this:
java.util.regex.Pattern.quote(target)
However, the better option might be, to just not use the regex-using replaceAll function but simply replace. Then you do not need to escape anything.
String.replaceAll() takes a regular expression and so it's trying to expand these metacharacters.
One approach is to escape these chars (e.g. \*).
Another would be to do the replacement yourself by using String.indexOf() and finding the start of the contained string. indexOf() doesn't take a regexp but rather a normal string.