This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 9 years ago.
Here If I am given a string aaaa|bbb, I want output as aaaa and bbb. If I use string.split("|") It returns each character of the string as separate Array of strings like
output[0]="a",output[1]="a",output[2]="a",output[3]="a",output[4]="a",output[5]="|",output[6]="b",output[&]="b",output[0]="b"
But I want it as output[0]=aaaa, output[1]=bbb;
Please help me
split() expects a regex, where | has a special meaning and you need to escape it.
string.split("\\|")
You need to escape the | metacharacter:
string.split("\\|")
Use proper escaping: string.split("\\|") or the helper function which has been created for exactly this purpose: string.split(Regexp.quote("|"))
Related
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I have timestamps in a field and would like to remove the 'T' and the 'Z' in the value. An example value is 2019-11-01T14:47:43Z. I would like to use a RegEx to solve this problem. I plan to use this in Java.
You can use Java's String.replaceAll() function to remove values with regex. The regular expression [a-zA-Z] will match any one letter; replacing it with an empty string will remove it entirely.
String ts = "2019-11-01T14:47:43Z";
System.out.println(ts.replaceAll("[a-zA-Z]", ""));
2019-11-0114:47:43
Demo
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I want to replace &sp; in the string below with Z.
Input text : ABCD&sp;EF&p;GHIJ&bsp;KL
Output text : ABCDZEFZGHIZKL
Can anyone tell me how to replace the every instance of &\D+; using java regular expression?
I am using /(&\D+;)?/ but it doesn't work.
Use String#replaceAll.
You also should use the ? modificator to +:
String str = "ABCD&sp;EF&p;GHIJ&bsp;KL";
String regex = "&\\D+?;";
System.out.println (str.replaceAll(regex,"Z"));
This should work
Match the initial &, then all characters that are not the tailing ;, then that tailing ; like so: &[^;]+; If not matching numbers (as suggested by your example with \D) is a requirement, add the numbers to the negated character set: [^;0-9] To make it replace all occurrences, add the global flag g. The site regexr.com is a handy tool to create regexes.
Edit: Sorry, I initially read your question wrong.
This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 5 years ago.
Regular Expressions in java
String s1="Anil-anilorg|anotherorg";
String s2="Anil-anilorg|";
I want to find weather s2 is present or sub-string of s1 by using regular expressions, but while I am doing that it is considering this symbol "|" as logical OR
I am using hbaseStringRegexComparator to compare
You need to escape the | as \|, and within a String it becomes "\\|".
You can use String.contains method. No regex needed:
String s1="Anil-anilorg|anotherorg";
String s2="Anil-anilorg|";
System.out.println(s1.contains(s2));
This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I want to split an android string to smaller ones with any | char.
Just imagine I have this long string :
This|is|a|long|string|in|java
So, I wanna split it. I need to get a array in output with this values :
[1]=>"This"
[2]=>"is"
[3]=>"a"
[4]=>"long"
[5]=>"string"
[6]=>"in"
[7]=>"java"
I have tried :
separated = oldstring.split("|");
But, i didn't give me the thing i need!
How can i do that? Any code can do that?
Note that String's split() method take regex as a param. Not string.
public String[] split(String regex)
Since | is a meta character, and it's have a special meaning in regex.
It works when you escape that.
String separated[] = oldstring.split("\\|");
This question already has answers here:
Why does String.split need pipe delimiter to be escaped?
(3 answers)
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 8 years ago.
Java string split: If I split "0" with delimiter "|" I expect array of one element as in ["0"]. But what I actually get is {"","0"}
How to fix this? Thanks
Argument to string split is a regex not a string. So pipeline symbol does not mean pipeline symbol. Why does String.split need pipe delimiter to be escaped?
Using "\\|" worked