I am new to "REGEX".And this question is training and educational for regex in java.
I try to remove leading new line chars from a string.(I know we can do this by 'trim()',but I do not want use it).I use '^[\r\n]+' as regex,like this:
str = "\r\n\r\ntest";
str.replaceAll("^[\r\n]+", "");
System.out.print(str);
I guess result will be
test
But the result is:
CRLF
CRLF
test
As you can see,leading new line chars do not removed.Also I try '^\s' or '^\s+' as regex,but result was same.Do you know why those regexes do not match leading new line chars?
A String cannot be modified: replaceAll returns the changed string.
str = str.replaceAll(...);
Related
String str = "hello there, what are you doing";
System.out.println(str.replaceAll("\\s{2,}", "?"));
output: hello?there what are?you doing
expected output: hello??there what are???you doing
Try this.
Pattern pat = Pattern.compile("\\s{2,}");
String str = "hello there, what are you doing";
System.out.println(pat.matcher(str).replaceAll(m -> "?".repeat(m.group().length())));
output:
hello??there, what are???you doing
A one line regex which uses replaces whitespace using look ahead (?=\s) and look behind (?<=\s) would be:
String str = "hello there, what are you doing here";
System.out.println(str.replaceAll("(\s(?=\s)|(?<=\s)\s)", "?"));
=>
hello??there, what are???you doing??????here
So the above matches whitespace followed by whitespace , or whitespace preceeded by whitespace and replaces this with "?".
I have a string "Rush to ER/F07^e80c801e-ee37-4af8-9f12-af2d0e58e341".
I want to split it into 2 strings on the delimiter ^. For example string str1=Rush to ER/F07 and String str2 = e80c801e-ee37-4af8-9f12-af2d0e58e341
For getting this i am doing splitting of the string , I followed the tutorial on stackoverflow but it is not working for me , here is a code
String[] str_array = message.split("^");
String stringa = str_array[0];
String stringb = str_array[1];
when I am printing these 2 strings I am getting nothing in stringa and in stringb I am getting all the string as it was before the delimiter.
Please help me
You have to escape special regex sign via \\ try this:
String[] str_array = message.split("\\^");
It is because the .split() method requires a regex pattern. Escape the ^:
String[] str_array = message.split("\\^");
You can get more information on this at http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-.
I have string like this T 8.ESTÜTESTतुम मेरी. Now using java regex i want to replace non-ascii character Ü, तुम मेरी with its equivalent code.
How can i achieve this?
I can replace it with any other string.
String str = "T 8.ESTÜTESTतुम मेरी";
String resultString = str.replaceAll("[^\\p{ASCII}]", "");
System.out.println(resultString);
It prints T 8.ESTTEST
Sorry, I don't know how to do this using a single regex, please check if this works for you
String str = "T 8.ESTÜTESTतुम मेरी";
StringBuffer sb = new StringBuffer();
for(int i=0;i<str.length();i++){
if (String.valueOf(str.charAt(i)).matches("[^\\p{ASCII}]")){
sb.append("[CODE #").append((int)str.charAt(i)).append("]");
}else{
sb.append(str.charAt(i));
}
}
System.out.println(sb.toString());
prints
T 8.EST[CODE #220]TEST[CODE #2340][CODE #2369][CODE #2350] [CODE #2350][CODE #2375][CODE #2352][CODE #2368]
the problem seems to be how to tell regex how to convert what it finds to the code.
I am using java replaceAll() method to escape new line characters
String comment = "ddnfa \n \r \tdnfadsf ' \r t ";
comment = comment.replaceAll("(\\n|\\r|\\t)","\\\\$1");
System.out.println(comment);
But the above code is still inserting new line.
Is there a way to output the comment exactly the same (i.e. with \n and \r instead of inserting new line)?
UPDATE:
I ended up using:
comment = comment.replaceAll("\\n","\\\\n")
.replaceAll("\\r","\\\\r")
.replaceAll("\\t","\\\\t");
You'll have to go one-by-one, since the new-line character U+000A has nothing to do with the two-character escape sequence \n:
comment = comment.replaceAll("\n","\\\\n");
comment = comment.replaceAll("\r","\\\\r");
comment = comment.replaceAll("\t","\\\\t");
you will have to do it character by character:
comment = comment.replaceAll("\n","\\\\n");
comment = comment.replaceAll("\r","\\\\r");
comment = comment.replaceAll("\t","\\\\t");
another solution is to escape the String as a Java String using this function:
comment = org.apache.commons.lang.StringEscapeUtils.escapeJava(comment);
This will make the String look exactly like the String in the Java Code, but it will also show other escape sequences (like \\, \" etc).
But maybe thats exactly what you want
Hard way: using Matcher
String comment = "ddnfa \n \r \tdnfadsf ' \r t ";
Map<String,String> sub = new HashMap<String,String>();
sub.put("\n", "\\\\n");
sub.put("\r", "\\\\r");
sub.put("\t", "\\\\t");
StringBuffer result = new StringBuffer();
Pattern regex = Pattern.compile("\\n|\\r|\\t");
Matcher matcher = regex.matcher(comment);
while (matcher.find()) {
matcher.appendReplacement(result, sub.get(matcher.group()));
}
matcher.appendTail(result);
System.out.println(result.toString());
prints
ddnfa \n \r \tdnfadsf ' \r
Why you dont use Matcher.quoteReplacement(stringToBeReplaced);?
It is a \ problem, simplify like this :
comment = comment.replaceAll("(\n|\r|\t)", "");
output :
ddnfa dnfadsf ' t
Try this..
comment.replaceAll("(\n)|(\r)|(\t)", "\n");
I want split a string like this:
C:\Program\files\images\flower.jpg
but, using the following code:
String[] tokens = s.split("\\");
String image= tokens[4];
I obtain this error:
11-07 12:47:35.960: E/AndroidRuntime(6921): java.util.regex.PatternSyntaxException: Syntax error U_REGEX_BAD_ESCAPE_SEQUENCE near index 1:
try
String s="C:\\Program\\files\\images\\flower.jpg"
String[] tokens = s.split("\\\\");
In java(regex world) \ is a meta character. you should append with an extra \ or enclose it with \Q\E if you want to treat a meta character as a normal character.
below are some of the metacharacters
<([{\^-=$!|]})?*+.>
to treat any of the above listed characters as normal characters you either have to escape them with '\' or enclose them around \Q\E
like:
\\\\ or \\Q\\\\E
You need to split with \\\\, because the original string should have \\. Try it yourself with the following test case:
#Test
public void split(){
String s = "C:\\Program\\files\\images\\flower.jpg";
String[] tokens = s.split("\\\\");
String image= tokens[4];
assertEquals("flower.jpg",image);
}
There is 2 levels of interpreting the string, first the language parser makes it "\", and that's what the regex engine sees and it's invalid because it's an escape sequence without the character to escape.
So you need to use s.split("\\\\"), so that the regex engine sees \\, which in turn means a literal \.
If you are defining that string in a string literal, you must escape the backslashes there as well:
String s = "C:\\Program\\files\\images\\flower.jpg";
String[] tokens=s.split("\\\\");
Try this:
String s = "C:/Program/files/images/flower.jpg";
String[] tokens = s.split("/");
enter code hereString image= tokens[4];
Your original input text should be
C:\\Program\\files\\images\\flower.jpg
instead of
C:\Program\files\images\flower.jpg
This works,
public static void main(String[] args) {
String str = "C:\\Program\\files\\images\\flower.jpg";
str = str.replace("\\".toCharArray()[0], "/".toCharArray()[0]);
System.out.println(str);
String[] tokens = str.split("/");
System.out.println(tokens[4]);
}