Java Regex to replace specific character within parenthesis - java

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.

Related

Java regex for inserting text between braces

I have a String
a = "stringWithBraces()"
I want to create the following string
"stringWithBraces(text)"
How do I achieve this using regex?
I tried this :
a.replaceAll("\\(.+?\\)", "text");
But get this :
stringWithBraces()
You can use lookaheads and do something like this:
(?<=\().*?(?=\))
Live Demo
Thus doing this:
String a = "stringWithBraces()";
a = a.replaceAll("(?<=\\().*?(?=\\))", Matcher.quoteReplacement("text"));
System.out.println(a);
Outputs:
stringWithBraces(text)
Note that in relation to replaceAll() then the replacement string has some special character. So you should most likely use Matcher.quoteReplacement() in order to escape those and be safe.
You can use this :
a = a.replaceAll("\\((.*?)\\)", "(text)");
You have to replace every thing between parenthesis with (text)
+ requires at least one char, the ? added here means the shortest match, so "...(.)...(.)..." would not continue to find ".)...(.".
a.replaceAll("\\(.*?\\)", "(text)");
You might have intended replaceFirst; though I think not.
You might also let the dot . match new line chars, for mult-line matches,
using the DOT_ALL option (?s):
a.replaceAll("(?s)\\(.*?\\)", "(text)");

Java: RegEx Multiple Matches in Same 'fragment'

I'm trying to write a RegEx that would capture both instance of 'csv' delimited by commas in the string "csv,csv,csv,csv" and replace with "xml".
So my output would be "csv,xml,xml,csv".
The problem I'm running into is that my RegEx only captures the first instance of csv, and since the 'comma' at the end of the first match would also be the first delimiter of the second match, it doesn't get captured.
RegEx = ",csv," (pretty simple example).
Example 2:
RegEx = "(,dog,)"
Input "dog,dog,dog,dog"
Output "dog,cat,cat,dog"
Edit: I found what I was looking for, using lookahead and lookbehind.
RegEx = "(?<=,)dog(?=,)" would do the trick.
Use look arounds, which assert, but don't consume:
String replaced = str.replaceAll("(?<=,)csv(?=,)", "xml");
Make use of lookaround:
replace
(?<=,)xml(?=,)
with
csv
Demo https://regex101.com/r/ft7SQT/1

Validate string that contains template in regex

I have a problem trying to validate this string...
So, the user selects a template: q( ). Then, the user fills in the contents inside the brackets which can end up like this:
q(a,b,c)
I have tried different ways using regex to validate this String, but it keeps returning the answer "No". I believe the problem is "q(" and ")" in my regex as I am not sure how it should look like.
Here's a snippet of the code:
String data2 = "q(a,b,c)";
String regex2 = "q([a-zA-Z0-9,'])";
if(data2.matches(regex2)){
System.out.println("yes");
}
else{
System.out.println("No");
}
I do have an alternative way by removing "q(" and ")" in data2 string, but I rather have it in regex without the need of removing characters in a String.
Any suggestions?
You need to escape the parentheses (and escape the escape character so that it compiles) and add a + at the end to indicate one or more characters
String regex2 = "q\\([a-zA-Z0-9,']+\\)";
You can read the meaning of every character in a regular expression in the Pattern javadoc.

how to remove only charactres in a given string in java?

I am trying to remove only [A-z|a-z] like this:
String input ="A021001208A 711100609C 01111";
String clean = input.replaceAll("\\D+^\\s+","");
System.out.println(clean.toString());
but the above code also removes the spaces; I don't want to remove space.
The expected output is:
021001208 711100609 01111
Please help me to formate the reg-ex to remove only characters.
Just replace [a-zA-Z] then:
String clean = input.replaceAll("(?i)[A-Z]+","");
(?i) is ignore case embedded flag expression.
Rather than use a positive character class, use a negated one. The regex you want is:
[^\d\s]
Which means "any character other than a digit or a whitespace".
When coded as java, it looks like:
String clean = input.replaceAll("[^\\d\\s]","");
Try this it will replace all occurence of alphabet from the given string.
String clean = input.replaceAll("[^a-zA-Z]", "");
You have to use [a-zA-Z] regular expression. So your .replaceAll() method will look like as below :
String clean = input.replaceAll("[a-zA-Z]","");

Regex required to update a character

I have a String : testing<b>s<b>tringwit<b>h</b>nomean<b>s</b>ing
I want to replace the character s with some other character sequence suppose : <b>X</b> but i want the character sequence s to remain intact i.e. regex should not update the character s with a previous character as "<".
I used the JAVA code :
String str = testing<b>s<b>tringwit<b>h</b>nomean<b>s</b>ing;
str = str.replace("s[^<]", "<b>X</b>");
The problem is that the regex would match 2 characters, s and following character if it is not ">" and Sting.replace would replace both the characters. I want only s to be replaced and not the following character.
Any help would be appreciated. Since i have lots of such replacements i don't want to use a loop matching each character and updating it sequentially.
There are other ways, but you could, for example, capture the second character and put it back:
str = str.replaceAll("s([^<])", "<b>X\\1</b>");
Looks like you want a negative lookahead:
s(?!<)
String str = "testing<b>s<b>tringwit<b>h</b>nomean<b>s</b>ing;";
System.out.println(str.replaceAll("s(?!<)", "<b>X</b>"));
output:
te<b>X</b>ting<b>s<b>tringwit<b>h</b>nomean<b>s</b>ing;
Use look arounds to assert, but not capture, surrounding text:
str = str.replaceAll("s(?![^<]))", "whatever");
Or, capture and put back using a back reference $1:
str = str.replaceAll("s([^<])", "whatever$1");
Note that you need to use replaceAll() (which use regex), rather than replace() (which uses plain text).

Categories