Having following string:
String value = "/cds/horse/schema1.0.0/day=12321/provider=samsung/run_key=32ee/group_key=222/end_date=2020-04-20/run_key_default=32sas1/somethingElse=else"
In need to replace values of run_key and run_key_default with %, for example, for above string result output will be the:
"/cds/horse/schema1.0.0/day=12321/provider=samsung/run_key=%/group_key=222/end_date=2020-04-20/run_key_default=%/somethingElse=else"
I would like to avoid mistakenly modifying other values, so in my opinion the best solution for it is combining replaceAll method with regex
String output = value.replaceAll("\run_key=[*]\", "%").replaceAll("\run_key_default=[*]\", "%")
I'm not sure how should I construct regex for it?
Feel free to post if you know better solution for it, than this one which I provided.
You may use this regex for search:
(/run_key(?:_default)?=)[^/]*
and for replacement use:
"$1%"
RegEx Demo
Java Code:
String output = value.replaceAll("(/run_key(?:_default)?=)[^/]*", "$1%");
RegEx Details:
(: Start capture group #1
/run_key: Match literal text /run_key
(?:_default)?: Match _default optionally
=: Match a literal =
): End capture group #1
[^/]*: Match 0 or more of any characters that is not /
"$1%" is replacement that puts our 1st capture group back followed by a literal %
public static void main(String[] args) {
final String regex = "(run_key_default|run_key)=\\w*"; //regex
final String string = "/cds/horse/schema1.0.0/day=12321/provider=samsung/run_key=32ee/group_key=222/end_date=2020-04-20/run_key_default=32sas1/somethingElse=else";
final String subst = "$1=%"; //group1 as it is while remaining part with %
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
output
Substitution result:
/cds/horse/schema1.0.0/day=12321/provider=samsung/run_key=%/group_key=222/end_date=2020-04-20/run_key_default=%/somethingElse=else
Related
I want a regex to remove all instances of dollar signs, commas, and opening and closing parentheses so that the String can be parsed to a Double.
Exmaples are:
($108.34)
$39.60
1,388.80
The code:
#Parsed
#Replace(expression = "", replacement = "")
public Double extdPrice;
This may help, we delete all the elements in this list: , $ ( )
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "[(),$]";
final String string = "($108.34)\n"
+ "$39.60\n"
+ "1,388.80";
final String subst = "";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
\d{1,3}(\,\d\d\d)*(\.\d+)?
can match all number like your examples, but it can't match 123456(no comma).
result was
108.34
39.60
1,388.80
and you need replace comma
Regex Expression = [^0-9\\.]
is what you are looking for. It will match anything other than digits 0-9 and character .
So technically this regex will remove all extra characters like ( , $ USD and etc
Example: System.out.println("($123.89)".replaceAll("[^0-9\\.]", "")); will give an output 123.89
Test output:
($108.34) => 108.34
$39.60 => 39.60
1,388.80 => 1388.80
I have an issue in Java when trying to remove the characters from the end of a string. This has now become a generic pattern match issue that I cannot resolve.
PROBLEM = remove all pluses, minuses and spaces (not bothered about whitespace) from the end of a string.
Pattern myRegex;
Matcher myMatch;
String myPattern = "";
String myString = "";
String myResult = "";
myString="surname; forename--+ + --++ "
myPattern="^(.*)[-+ ]*$"
//expected result = "surname; forename"
myRegex = Pattern.compile(myPattern);
myMatch = myRegex.matcher(myString);
if (myMatch.find( )) {
myResult = myMatch.group(1);
} else {
myResult = myString;
}
The only way I can get this to work is by reversing the string and reversing the pattern match, then I reverse the result to get the right answer!
In the following pattern:
^(.*)[-+ ]*$
... the .* is a greedy match. This means that it will match as many characters as possible while still allowing the entire pattern to match.
You need to change it to non-greedy by adding ?.
^(.*?)[-+ ]*$
I have this code to find this pattern: 201409250200131738007947036000 - 1 ,inside the text
final String patternStr = "(\\d{30} - \\d{1})";
final Pattern p = Pattern.compile(patternStr);
final Matcher m = p.matcher(page);
if (m.matches()) {
System.out.println("SUCCESS");
}
But for any strange reasson in Java did't work, Can somebody help me where is the error please?
The reason is that the matches method checks for the entire given string to match the regex.
So i.e. if your string is 123456123412345612341234561234 - 8 it will match, if it is my number 123456123412345612341234561234 - 8 is inside other text it won't.
Use the find method to accomplish your task:
if (m.find()) {
System.out.println("SUCCESS");
}
It will search inside the given string instead of attempting to match the entire string.
From the documentation for Matcher, matches:
Attempts to match the entire region against the pattern.
As opposed to find which:
Attempts to find the next subsequence of the input sequence that matches the pattern.
So use matches to match an entire String against a pattern, use find to locate a pattern inside a String.
Try:
final String patternStr = "\\d{30}+\\s-\\s\\d";
final Pattern p = Pattern.compile(patternStr);
final Matcher m = p.matcher(page);
while (m.find()) {
System.out.printf("FOUND A MATCH: %s%n", matcher.group());
}
I edited your pattern slightly to make it more robust. This will print each match that it finds.
Pattern is:
private static Pattern r = Pattern.compile("(.*\\..*\\..*)\\..*");
String is:
sentVersion = "1.1.38.24.7";
I do:
Matcher m = r.matcher(sentVersion);
if (m.find()) {
guessedClientVersion = m.group(1);
}
I expect 1.1.38 but the pattern match fails. If I change to Pattern.compile("(.*\\..*\\..*)\\.*");
// notice I remove the "." before the last *
then 1.1.38.XXX fails
My goal is to find (x.x.x) in any incoming string.
Where am I wrong?
Problem is probably due to greedy-ness of your regex. Try this negation based regex pattern:
private static Pattern r = Pattern.compile("([^.]*\\.[^.]*\\.[^.]*)\\..*");
Online Demo: http://regex101.com/r/sJ5rD4
Make your .* matches reluctant with ?
Pattern r = Pattern.compile("(.*?\\..*?\\..*?)\\..*");
otherwise .* matches the whole String value.
See here: http://regex101.com/r/lM2lD5
I have the following entry in a properties file:
some.key = \n
[1:Some value] \n
[14:Some other value] \n
[834:Yet another value] \n
I am trying to parse it using a regular expression, but I can't seem to get the grouping correct. I am trying to print out a key/value for each entry. Example: Key="834", Value="Yet another value"
private static final String REGEX_PATTERN = "[(\\d+)\\:(\\w+(\\s)*)]+";
private void foo(String propValue){
final Pattern p = Pattern.compile(REGEX_PATTERN);
final Matcher m = p.matcher(propValue);
while (m.find()) {
final String key = m.group(0).trim();
final String value = m.group(1).trim();
System.out.println(String.format("Key[%s] Value[%s]", key, value));
}
}
The error I get is:
Exception: java.lang.IndexOutOfBoundsException: No group 1
I thought I was grouping correctly in the regex but I guess not. Any help would be appreciated!
Thanks
UPDATE:
Escaping the brackets worked. Changed the pattern to the followingThanks for the feedback!
private static final String REGEX_PATTERN = "\\[(\\d+)\\:(\\w+(\\w|\\s)*)\\]+";
[ should be escaped (as well as ]).
"\\[(\\d+)....\\]+"
[] Is used for character classes: [0-9] == (0|1|2|...|9)
Try this:
private static final String REGEX_PATTERN = "\\[(\\d+):([\\w\\s]+)\\]";
final Pattern p = Pattern.compile(REGEX_PATTERN);
final Matcher m = p.matcher(propValue);
while (m.find()) {
final String key = m.group(1).trim();
final String value = m.group(2).trim();
System.out.println(String.format("Key[%s] Value[%s]", key, value));
}
the [ and ] need to be escaped because they represent the start and end of a character class
group(0) is always the full match, so your groups should start with 1
note how I wrote the second group [\\w\\s]+. This means a character class of word or whitespace characters
It's your regex, [] are special characters and need to be escaped if you want to interpret them literally.
Try
"\\[(\\d+)\\:(\\w+(\\s)*)\\]"
Note - I removed the '+'. The matcher will keep finding substrings that match the pattern so the + is not necessary. (You might need to feed in a GLOBAL switch - I can't remember).
I can't help but feel this might be simpler without regex though, perhaps by splitting on \n or [ and then splitting on : for each of those.
Since you are using string that consists of several lines you should tell it to Pattern:
final Pattern p = Pattern.compile(REGEX_PATTERN, Pattern.MULTILINE);
Although it is irrelevant directly for you I'd recommend you to add DOTALL too:
final Pattern p = Pattern.compile(REGEX_PATTERN, Pattern.MULTILINE | Pattern.DOTALL);