I have a string with the following format:
String str = "someString(anotherString)(lastString)";
I wanted to replace the lastString inside the last brackets, i.e new String should be
newStr = "someString(anotherString)(modified)";
I am using regex with "\\(([^\\}]+)\\)$" pattern.
But I am unable to change only the last content inside brackets.
The above regex gives me the output:
"someString(modified)";
I just want to replace the content of the last brackets, any characters can appear infront of last bracket.
ANy help is appreciated.
I think you have a typo in your expression. Replace the curly brackets with a regular one, and I think it will be fine.
yourString.replaceAll("(.+\\(.+\\)\\()[^\\}]+(\\)$)", "$1modified$2")
String resultString = subjectString.replaceAll(
"(?x)\\( # match opening parenthesis\n" +
"[^()]* # match 0 or more characters except parentheses\n" +
"\\) # match closing parenthesis\n" +
"$ # match the end of the string", "(modified)");
So far, this is not allowing for whitespace between the closing parenthesis and the end of the string. You might want to insert a \\s* before the $ if you need to handle that case, too.
Related
I am using regex in this line of code:
if (line.matches("^.*\b(" + incomingMachineIP + ")\b.*$"))
So, assume the following inputs:
incomingMachineIp = "10.10.10.10"
line = "abcde=10.10.10.10abcedf"
I want the if condition to return true, which is what I'm trying to do with the regex above however a false is still returned even though the value of incomingMachineIp is found in the string (line)
Any help how to resolve this would be greatly appreciated.
In your example you can simply use line.contains(incomingMachineIP) and don't have to deal with regex patterns at all.
You need to escape the backslash one more times and also you need to remove the second \b or replace with \B because there isn't a word boundary exists between the last 0 and the letter a
if(line.matches(".*\\b" + Pattern.quote(incomingMachineIP) + ".*"))
Since you're using matches method, it won't require anchors.
use a live Regex Console to test your expressions until you have got it working. Just remember to replace any single character backslash "\" with a double backslash character "\\" before using your expression in java!
I have a database with 50000 rows. I want to get all rows one by one, put them into a String variable and delete all characters between "(" and ")", then update the row. I just don't know how to delete all characters between "(" and ")". I want to do this with java.
Your regular expression would be something like:
data.replaceAll("\\(.*?\\)", "()"); //if you want to keep the brackets
OR
data.replaceAll("\\(.*?\\)", ""); //if you don't want to keep the brackets
The string consists of 3 parts:
\\( //This part matches the opening bracket
.*? //This part matches anything in between
\\) //This part matches the closing bracket
I hope this helps
Use the replaceall(string regex, string replacement) statement, like this:
data.replaceall(" and ", "");
Need help in getting the java regex to replace = sing between parenthesis with #, my input text is
8=FIX.4.&49=(550=0449)&35=RIO&76=(AB=4560)&
expected output string
8=FIX.4.&49=(550#0449)&35=RIO&76=(AB#4560)&
So would like to replace = char only within (550=0449) and (AB=4560) with # so the output should contain (550#0449) and (AB#4560).
I like anubhava's answer, but if you want to be more strict and assert there are non-blank terms and opening and closing brackets, capture the terms and write them back using back references:
str = str.replaceAll("(\\(\\w+)=(\\w+\\))", "$1#$2");
You can use:
String repl = str.replaceAll("=(?=[^()]*\\))", "#");
(?=[^()]*\)) is a lookahead that will make sure to match = only when there is a ) following it.
In the end I need a regex which basically converts me a phone number into a E164 conform number. As for now i got this:
result = s.replaceAll("[(*)|+| ]", "");
It replaces everything fine: the spaces, the "+"-sign and also the braces "()". But it does not match the content of its braces, so that e.g. the number +49 (0)11 111 11 11 will be replaced to 49111111111.
How can I get this to work?
You can do it, but what if there's more than just a zero between parentheses?
result = s.replaceAll("\\([^()]*\\)|[*+ ]+", "");
As a verbose regex:
result = s.replaceAll(
"(?x) # Allow comments in the regex. \n" +
"\\( # Either match a ( \n" +
"[^()]* # then any number of characters except parentheses \n" +
"\\) # then a ). \n" +
"| # Or \n" +
"[*+\\ ]+ # Match one or more asterisks, pluses or spaces", "");
[(*)|+| ]
is a character class, matching any single parenthesis, asterisk, bar, plus or space character. Get rid of the square brackets and use something like
s.replaceAll("\\(.*?\\)|\\D", "");
This will remove anything between (and including) parentheses, as well as anything else that is not a digit. Note that this will not handle nested parentheses very well - it will eat everything from an open parenthesis to the first closing one it finds, so would change (123(45)67) into 67 (the unbalanced close parenthesis being removed as it's a \D)
You might try this: "(\\(\\d+\\))|\\+|\\s". Removes the paren's and contents, plus sign, and space.
I think you are expecting a little too much magic from character classes. Firstly, in character classes, don't use |. It is just another character that will be matched by the character class. Simply list all the characters you want to include without any delimiters.
Secondly, a character class really just matches single characters. So (*) inside a character class can by definition do nothing more than remove (, or * (literally) or ). If you are 100% sure that your input will never have nested parentheses or unmatched parentheses or something, then you can do something like this:
"(?:\\([^)]\\)|\\D)+"
I have a string:
HLN (Formerly Headline News)
I want to remove everything inside the parens and the parens themselves, leaving only:
HLN
I've tried to do this with a regex, but my difficulty is with this pattern:
"(.+?)"
When I use it, it always gives me a PatternSyntaxException. How can I fix my regex?
Because parentheses are special characters in regexps you need to escape them to match them explicitly.
For example:
"\\(.+?\\)"
String foo = "(x)()foo(x)()";
String cleanFoo = foo.replaceAll("\\([^\\(]*\\)", "");
// cleanFoo value will be "foo"
The above removes empty and non-empty parenthesis from either side of the string.
plain regex:
\([^\(]*\)
You can test here: http://www.regexplanet.com/simple/index.html
My code is based on previous answers
You could use the following regular expression to find parentheticals:
\([^)]*\)
the \( matches on a left parenthesis, the [^)]* matches any number of characters other than the right parenthesis, and the \) matches on a right parenthesis.
If you're including this in a java string, you must escape the \ characters like the following:
String regex = "\\([^)]*\\)";
String foo = "bar (baz)";
String boz = foo.replaceAll("\\(.+\\)", ""); // or replaceFirst
boz is now "bar "