Why String.replaceAll() don't work on this String? - java

//This source is a line read from a file
String src = "23570006,music,**,wu(),1,exam,\"Monday9,10(H2-301)\",1-10,score,";
//This sohuld be from a matcher.group() when Pattern.compile("\".*?\"")
String group = "\"Monday9,10(H2-301)\"";
src = src.replaceAll("\"", "");
group = group.replaceAll("\"", "");
String replacement = group.replaceAll(",", "##");
System.out.println(src.contains(group));
src = src.replaceAll(group, replacement);
System.out.println(group);
System.out.println(replacement);
System.out.println(src);
I'm trying to replace the "," between \"s so I can use String.split() latter.
But the above just not working , the result is:
true
Monday9,10(H2-301)
Monday9##10(H2-301)
23570006,music,**,wu(),1,exam,Monday9,10(H2-301),1-10,score,
but when I change the src string to
String src = "123\"9,10\"123";
String group = "\"9,10\"";
It works well
true
9,10
9##10
1239##10123
What's the matter with the string???

( and ) are regex metacharacter; they need to be escaped if you want to match it literally.
String group = "\"Monday9,10\\(H2-301\\)\"";
^ ^
The reason why you need two slashes is that because \ in a string literal is itself an escape character, so "\\" is a string of length 1 containing a slash.

Related

Wildcard match and replace in Java

I want to check a string to see if it contains $wildcard$, and ONLY if it does I want to extract the value between the "$ $", which I'll use to retrieve a replacement. Then replace the full new string (removing the $ $ as well)
Edit: managed to get this working demo
String subject = "test/$name$/something";
String replace = "foo_bar";
Pattern regex = Pattern.compile("(\\$).*?(\\$)");
Matcher m = regex.matcher(subject);
StringBuffer b= new StringBuffer();
while (m.find()) {
String something = m.group(0);
System.out.println(something);
m.appendReplacement(b, replace);
}
m.appendTail(b);
String replaced = b.toString();
System.out.println(replaced);
Gives me the output of
$name$
test/foo_bar/something
I could substring to remove the lead/trailing $ but is there a way to split these into groups so I can just get what is between $ $. But also ensuring that the initial check ensures it has a start and end $
Add another matching group for the content of the tag:
Pattern.compile("(\\$)(.*?)(\\$)");
Remove unnecessary capturing groups from both \\$, set the capturing group on the pattern that matches what is between two $ chars (and the most efficient construct to use here is a negated character class [^$]), and then just grab the value of .group(1):
String subject = "test/$name$/something";
String replace = "foo_bar";
Pattern regex = Pattern.compile("\\$([^$]*)\\$"); // ONLY 1 GROUP ROUND [^$]*
Matcher m = regex.matcher(subject);
StringBuffer b= new StringBuffer();
while (m.find()) {
String something = m.group(1); // ACCESS GROUP 1
System.out.println(something);
m.appendReplacement(b, replace);
}
m.appendTail(b);
String replaced = b.toString();
System.out.println(replaced);
See the Java demo
Result:
name
test/foo_bar/something
Pattern details
\\$ - a $ char
([^$]*) - Capturing group 1 matching zero or more chars other than $ char
\\$ - a $ char.
It has slightly different syntax that you're asking for, but check out Apache Commons Text: https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/StrSubstitutor.html
This will let you do things like:
Map<String,String> substitutions = ImmutableMap.of("name", "foo_bar");
String template = "/test/${name}/something";
StrSubstitutor substitutor = new StrSubstitutor(substitutions);
System.out.println(substitutor.replace(template));
You could build your own Map to populate with your substitution values.

How to extract a string till the end of the line with regular expression

I have the following string(contains Portuguese characters) in the following structure: contain Name: and then some words after.
Example:
String myStr1 = "aaad Name: bla and more blá\n gdgdf ppp";
String myStr2 = "bbbb Name: Á different blÁblÁ\n hhhh fjjj";
I need to extract the string from 'Name:' till the end of the line.
example:
extract(myStr1) = "Name: bla and more blá"
extract(myStr2) = "Name: Á different blÁblÁ"
Edit after #blue_note answer:
here is what I tried:
public static String extract(String myStr) {
Pattern p = compile("Name:(?m)^.*$");
Matcher m = p.matcher(myStr);
while (m.find()) {
String theGroup = m.group(0);
System.out.format("'%s'\n", theGroup);
return m.group(0);
}
return null;
}
did not work.
The regex is "^\\w*\\s*((?m)Name.*$)")
where
?m enables the multiline mode
^, $ denote start of line and end of line respectively
.* means any character, any number of times
And get group(1), not group(0) of the matched expression
You could also use substring in this case:
String name = myStr1.substring(myStr1.indexOf("Name:"), myStr1.indexOf("\n"));

Android Java - String .replaceAll to replace specific characters (regex)

I need to remove some specific "special" characters and replace them with empty string if they show up.
I am currently having a problem with the regex, probably with the Java escaping. I can't put them all together, it just doesn't work, I tried a lot! T_T
Currently I am doing it one by one which is kinda silly, but for now at least it works, like that :
public static String filterSpecialCharacters(String string) {
string = string.replaceAll("-", "");
string = string.replaceAll("\\[", "");
string = string.replaceAll("\\]", "");
string = string.replaceAll("\\^", "");
string = string.replaceAll("/", "");
string = string.replaceAll(",", "");
string = string.replaceAll("'", "");
string = string.replaceAll("\\*", "");
string = string.replaceAll(":", "");
string = string.replaceAll("\\.", "");
string = string.replaceAll("!", "");
string = string.replaceAll(">", "");
string = string.replaceAll("<", "");
string = string.replaceAll("~", "");
string = string.replaceAll("#", "");
string = string.replaceAll("#", "");
string = string.replaceAll("$", "");
string = string.replaceAll("%", "");
string = string.replaceAll("\\+", "");
string = string.replaceAll("=", "");
string = string.replaceAll("\\?", "");
string = string.replaceAll("|", "");
string = string.replaceAll("\"", "");
string = string.replaceAll("\\\\", "");
string = string.replaceAll("\\)", "");
string = string.replaceAll("\\(", "");
return string;
}
Those are all the character I need to remove:
- [ ] ^ / , ' * : . ! > < ~ # # $ % + = ? | " \ ) (
I am clearly missing something, I can't figure out how to put it all in one line. Help?
Your code does not work in fact because .replaceAll("$", "") replaces an end of string with empty string. To replace a literal $, you need to escape it. Same issue is with the pipe symbol removal.
All you need to do is to put the characters you need to replace into a character class and apply the + quantifier for better performance, like this:
string = string.replaceAll("[-\\[\\]^/,'*:.!><~##$%+=?|\"\\\\()]+", "");
Note that inside a character class, most "special regex metacharacters" lose their special status, you only have to escape [, ], \, a hyphen (if it is not at the start/end of the character class), and a ^ (if it is the first symbol in the "positive" character class).
DEMO:
String s = "-[]^/,'*:.!><~##$%+=?|\"\\()TEXT";
s = s.replaceAll("[-\\[\\]^/,'*:.!><~##$%+=?|\"\\\\()]+", "");
System.out.println(s); // => TEXT
Use these codes
String REGEX = "YOUR_REGEX";
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(yourString);
yourString = m.replaceAll("");
UPDATE :
Your REGEX looks something like
String REGEX = "-|\\[|\\]|\\^|\\/|,|'|\\*|\\:|\\.|!|>|<|\\~|#|#|\\$|%|\\+|=\\?|\\||\\\\|\\\\\\\\|\\)|\\(";
SAPMLE :
String yourString = "#My (name) -is #someth\ing"";
//Use Above codes
Log.d("yourString",yourString);
OUTPUT

Regex for string between quotes and replace it

Can you guys help me??
I have a string here :
a$20=A.createVar("/LIST/S_UNB/C_S001/D_0001/*var", a$1, this);
Now I want to replace the string in "" with the value that appears after last '/'.
here I want result to be
a$20=A.createVar("*var", a$1, this);
I am trying to use as minimal objects as possible and my regex looks like this
\"([^\"]*)\"
Is this correct?
Assuming the quotes aren't part of the expression, use
[^/]+$
$ signifies the end of the string, which will make it return only the value after the last '/'.
You can use this code:
String s = "a$20=A.createVar(\"/LIST/S_UNB/C_S001/D_0001/*var\", a$1, this);";
// extract text between ""
String sub = s.replaceAll("^[^\"]*\"([^\"]*)\".*$", "$1");
// find last index of /
int i = sub.lastIndexOf('/');
// replace content between "" by token after last /
String repl = s.replaceFirst("\"[^\"]*\"", '"' + sub.substring(i+1) + '"');
//=> a$20=A.createVar("*var", a$1, this);

String manipulation questions

I have a string like "C:\Program Files\Directory\Something.txt" and I would like to convert it into "C:\\Program Files\\Directory\\Something.txt" So basically add \ wherevever a \ is found. Is the best way to approach this using indexOf and breaking the string into sustrings and then concatenating again or is there a more efficent way of doing this in Java?
String s = "C:/Program Files/Directory/Something.txt";
String s2 = s.replaceAll("/", "//");
// => "C://Program Files//Directory//Something.txt"
[Edit]
If the string has backslashes then it gets really fun since that is the regular expression escape character. This should work:
String s = "C:\\Program Files\\Directory\\Something.txt";
s.replaceAll("\\\\", "\\\\\\\\");
// => "C:\\\\Program Files\\\\Directory\\\\Something.txt"
Note that's four backslashes in the regex (two pairs in sequence to get two literal backslashses) and then eight in the replacement string since backslashes are also escape characters for literal strings.
You could use the File.separator property to make it more cross-platform:
String input = "C:/Program Files/Directory/Something.txt";
String result = input.replaceAll(File.separator, File.separator + File.separator);
You can just use String.replaceAll for this.
String str = "C:/Program Files/Directory/Something.txt";
str = str.replaceAll("/","//");
String s = "C:/Program Files/Directory/Something.txt";
s = s.replace("/", "//");
This will replace all / in the string with //

Categories

Resources