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
Related
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
I have this String:
String filename = 20190516.BBARC.GLIND.statistics.xml;
How can I get the first part of the String (numbers) without the use of substrings.
Here, we might just want to collect our digits using a capturing group, and if we wish, we could later add more boundaries, maybe with an expression as simple as:
([0-9]+)
For instance, if our desired digits are at the start of our inputs, we might want to add a start char as a left boundary:
^([0-9]+)
Or if our digits are always followed by a ., we can bound it with that:
^([0-9]+)\.
and we can also add a uppercase letter after that to strengthen our right boundary and continue this process, if it might be necessary:
^([0-9]+)\.[A-Z]
RegEx
If this expression wasn't desired, it can be modified or changed in regex101.com.
RegEx Circuit
jex.im visualizes regular expressions:
Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "([0-9]+)";
final String string = "20190516.BBARC.GLIND.statistics.xml";
final String subst = "\\1";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
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);
Demo
const regex = /([0-9]+)(.*)/gm;
const str = `20190516.BBARC.GLIND.statistics.xml`;
const subst = `$1`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
To extract a part or parts of string using regex I prefer to define groups.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class B {
public static void main(String[] args) {
String in="20190516.BBARC.GLIND.statistics.xml";
Pattern p=Pattern.compile("(\\w+).*");
Matcher m=p.matcher(in);
if(m.matches())
System.out.println(m.group(1));
else
System.out.println("no match");
}
}
Hi this is really troubling me, and I just cant figure out the regex.
I would like a regex that could replace n amount of consecutive whitespaces with '♥', but only if there i exactly n amount of whitespace and not more or not less.
Pseudo code:
String myReplaceFunction(String text, String replaceThis, String withThis, int countOfConcecutive);
String originalString =" This is a very short text . ";
String regexMagicString = myReplaceFunction(" ", "♥", 4);
System.out.println(regexMagicString); // "♥This is a very♥short♥text♥. "
This seems to work (no look-behind or look-ahead needed):
/(^|\S)\s{4}(\S|$)/
Just remember to replace it by $1♥$2.
Code
See regex in use here
(?<! ) {4}(?! )
Replacement: ♥
Usage
See code in use here
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
final String string = " This is a very short text . ";
final String replace = " ";
final String subst = "♥";
String result = myReplaceFunction(string, replace, subst, 4);
System.out.println(result);
}
public static String myReplaceFunction(String text, String replaceThis, String withThis, int countOfConsecutive) {
replaceThis = Pattern.quote(replaceThis);
final String regex = String.format("(?<!%1$s)%1$s{%2$s}(?!%1$s)", replaceThis, countOfConsecutive);
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(text);
final String result = matcher.replaceAll(withThis);
return "Substitution result: " + result;
}
}
Results
Note: The input contains 5 spaces after the . that don't get replaced.
Input
This is a very short text .
Output
♥This is a very♥short♥text♥.
Explanation
(?<! ) Negative lookbehind ensuring what precedes is not a space character
{4} Match the space character exactly 4 times
(?! ) Negative lookahead ensuring what follows is not a space character
As this whitespace probably excludes the line breaks \s is not possible.
s = s.replace("(?<![ \t])[ \t]{13}(?![ \t])", "♥");
For whitespace: [ \t].
Negative look-behind: (?<![ \t]) - zero-width.
Negative look-ahead: (?![ \t])
I did not test it.
I have a pretty long string that looks something like
{abc:\"def\", ghi:\"jkl\"}
I want to convert this to a valid json string like
{\"abc\":\"def\", \"ghi\":\"jkl\"}
I started looking at the replaceAll(String regex, String replacement) method on the string object but i'm struggling to find the correct regex for it.
Can someone please help me with this.
In this particular case the regex should look for a word that is proceeded with {, space, or , and not followed by "
String str = "{abc:\"def\", ghi:\"jkl\"}";
String regex = "(?:[{ ,])(\\w+)(?!\")";
System.out.println(str.replaceAll(regex, "\\\"$1\\\""));
DEMO and regex explanation
I have to make an assumption that the "key" and "value" consist of only
"word characters" (\w) and there are no spaces in them.
Here is my program. Please also see the comments in-line:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexJson {
public static void main(String[] args) {
/*
* Note that the input string, when expressed in a Java program, need escape
* for backslash (\) and double quote ("). If you read directly
* from a file then these escapes are not needed
*/
String input = "{abc:\\\"def\\\", ghi:\\\"jkl\\\"}";
// regex for one pair of key-value pair. Eg: abc:\"edf\"
String keyValueRegex = "(?<key>\\w+):(?<value>\\\\\\\"\\w+\\\\\\\")";
// regex for a list of key-value pair, separated by a comma (,) and a space ( )
String pairsRegex = "(?<pairs>(,*\\s*"+keyValueRegex+")+)";
// regex include the open and closing braces ({})
String regex = "\\{"+pairsRegex+"\\}";
StringBuilder sb = new StringBuilder();
sb.append("{");
Pattern p1 = Pattern.compile(regex);
Matcher m1 = p1.matcher(input);
while (m1.find()) {
String pairs = m1.group("pairs");
Pattern p2 = Pattern.compile(keyValueRegex);
Matcher m2 = p2.matcher(pairs);
String comma = ""; // first time special
while (m2.find()) {
String key = m2.group("key");
String value = m2.group("value");
sb.append(String.format(comma + "\\\"%s\\\":%s", key, value));
comma = ", "; // second time and onwards
}
}
sb.append("}");
System.out.println("input is: " + input);
System.out.println(sb.toString());
}
}
The print out of this program is:
input is: {abc:\"def\", ghi:\"jkl\"}
{\"abc\":\"def\", \"ghi\":\"jkl\"}
Example
input: abc def abc abc pqr
I want to to replace abc at third position with xyz.
output: abc gef abc xyz pqr
Thanks in advance
One way to do this would be to use.
String[] mySplitStrings = null;
String.Split(" ");
mySplitString[3] = "xyz";
And then rejoin the string, its not the best way to do it but it works, you could put the whole process into a function like.
string ReplaceStringInstance(Seperator, Replacement)
{
// Do Stuff
}
Group the three segments, that are the part before the replaced string, the replaced string and the rest and assemble the prefix, the replacement and the suffix:
String pattern = String.format("^(.*?%1$s.*?%1$s.*?)(%1$s)(.*)$", "abc");
String result = input.replaceAll(pattern, "$1xyz$3");
This solution assumes that the whole input is one line. If you have multiline input you'll have to replace the dots as they don't match line separators.
There's plenty of ways to do this, but here's one. It assumes that the groups of letters will be separated by spaces, and looks for the 3rd 'abc' block. It then does a single replace to replace that with 'xyz'.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class main {
private static String INPUT = "abc def abc abc pqr";
private static String REGEX = "((?:abc\\ ).*?(?:abc\\ ).*?)(abc\\ )";
private static String REPLACE = "$1xyz ";
public static void main(String[] args) {
System.out.println("Input: " + INPUT);
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT); // get a matcher object
INPUT = m.replaceFirst(REPLACE);
System.out.println("Output: " + INPUT);
}
}