I'm trying to figure out how to replace with Java 1.6 in strings like
hello ${world }! ${txt + '_t'}<br/> ${do_not_replace
any substring identified between '${' and '}' with the same substring without these delimiters.
So the output for the string above should be
hello world ! txt + '_t'<br/> ${do_not_replace
I identified a working pattern that allows me to replace the substrings with a fixed string
str.replaceAll('[${](.*?)}', '_')
and i know that i cannot use named groups with this version of Java.
Any suggestion for a simple solution to this problem are highly appreciated! Many thanks
try
s = s.replaceAll("\\$\\{(.+?)}", "$1");
Related
I'm trying to learn regex in Java.
So far, I've been trying some little mini challenges and I'm wondering if there is a way to define a nth character.
For instance, let's say I have this string: todayiwasnotagoodday
If I want to replace the third (fourth or seventh) character, how I can define a regex in order to change an specific "index", for this example the 'd' for an empty space "".
I've been searching about it, but so far my implementations match from the first element to the third: ^[a-z]{3}
¿Is it possible to define this regex?
Thanks in advance.
If you want to replace the third character with a space via regex, you could try a regex replace all:
String input = "todayiwasnotagoodday";
String output = input.replaceAll("^(.{2}).(.*)$", "$1 $2");
System.out.println(output); // to ayiwasnotagoodday
Note that you could also avoid regex here, and just use substring operations:
String output = input.substring(0, 2) + " " + input.substring(3);
System.out.println(output); // to ayiwasnotagoodday
I want to match below string
RegEx I'm trying:
{1:F21XXXXXXXX9999123456}{4:{177:1007300\\d{2}8}{451:0}{108:XXX190876234-1}}
{1:F21XXXXXXXX9999123456}{4:{177:1007300\\d+
String to match:
{1:F21XXXXXXXX9999123456}{4:{177:1007300838}{451:0}{108:XXX190876234-1}}
I tried other ways as well but facing issues in matching any help would be appreciated.
Your first regexp (with missing escapes) is:
\{1:F21XXXXXXXX9999123456\}\{4:\{177:1007300\d{2}8\}\{451:0\}\{108:XXX190876234-1\}\}
When written as a Java String literal:
Pattern.compile("\\{1:F21XXXXXXXX9999123456\\}\\{4:\\{177:1007300\\d{2}8\\}\\{451:0\\}\\{108:XXX190876234-1\\}\\}");
I am trying to replace all the hashtags in a tweet body using regex in java.
Firstly I used this regex #\\w+|\\s#\\w+ but this does not work with tweets contains Arabic characters so I moved to this one
#[\\x{0021}-\\x{007E}\\x{060C}-\\x{06DC}^\\s]+
which simply match any character between the two ranges u0021 to u007E and u060C to u06DC.
The second regex works fine but for a string like this "#شو_تعلمت_من_المدرسه ولاشي" it causes the whole string to be replaced.
What is the problem with that expression?
Is there any better regex rather than this ?
Why don't you just start at a hash and stop at a whitespace?
#[^\\s]*
Eliminating spaces and words starting with particular chars from JAVA string.
With the following code spaces between string words are eliminated:
String str1= "This is symbel for snow and silk. Grapes are very dear"
String str2=str1.replaceAll(" ","");
System.out.println(str2);
It gives this output:-
output:
Thisissymbelforsnowandsilk.Grapesareverydear
But I want to eliminate all the words in str1 starting with char 's' (symbel snow silk) and char 'd' (dear) to get the following output:-
output:
Thisisforand.Grapesarevery
How it can be achieved by amending this code?
The best solution is to use a Regular Expression also known as a Regex.
These are designed specifically for complex search and replace functionality in strings.
This one:
"([sd]\\w+)|\\s+"
matches a word group indicated by the parentheses () starting with 's' or 'd' followed by one or more "word" characters (\\w = any alpha numeric or underscore) OR one or more whitespace characters (\\s = whitespace). When used as an argument to the String replaceAll function like so:
s.replaceAll("([sd]\\w+)|\\s+", "");
every occurance that matches either of these two patterns is replaced with the empty string.
There is comprehensive information on regexes in Oracle's java documentation here:
http://docs.oracle.com/javase/tutorial/essential/regex/
Although they seem cryptic at first, learning them can greatly simplify your code. Regexes are available in almost all modern languages so any knowledge you gain about regexes is useful and transferable.
Furthermore, the web is littered with handy sites where you can test your regexes out before committing them to code.
Do like this
String str1= "This is symbel for snow and silk. Grapes are very dear";
System.out.print(str1.replaceAll("[sd][a-z]+|[ ]+",""));
Explanation
try this
s = s.replaceAll("([sd]\\w+)|\\s+", "");
I want to create a regex which matches a word in a String:
Miete 920
I want to match the word "Miete".
My regex:
price.matches("=[\bMiete\b]")
However, it doesn`t work? Pls give me a hint.
If you want to check if some string contains separate word Miete you can use
price.matches(".*\\bMiete\\b.*");
There is no need for = in your regex, also [...] is character class not string literal.
I think your regex is wrong. Try with
price.matches(".*\\bMiete\\b.*")
.* -> 0 or more charcters
\\b -> word boundary
So this will match any string that has Miete surrounded by word boundaries.
EDIT: sorry fixed, I forgot how matching works in Java, I'm more used to Perl :)