I have a string that contains the following:
"Hello my name is (0.9%) and (15%) bye (10.5%) also (C9, B6)"
I want to replaceAll so I get rid of the brackets containing percentages but not the other numbers like so:
"Hello my name is and bye also C9, B6"
Currently I have this but it removes all my numbers, Any idea how I could fix it:
.replaceAll("[\\([0-9]\\%)]","");
Something like this maybe?
"Hello my name is (0.9%) and (15%) bye (10.5%) also (C9, B6)".replaceAll("\\((?:\\d|\\.)+%\\)", "")
Demo
This here also deletes a single whitespace after each parenthesized percentage:
"Hello my name is (0.9%) and (15%) bye (10.5%) also (C9, B6)".replaceAll("\\((?:\\d|\\.)+%\\) ", "")
Gives:
Hello my name is and bye also (C9, B6)
Remove the outer brackets and add matching to digits before and after an optional dot.
.replaceAll("\\([0-9]+?\\.?[0-9]+?\\%\\)", "");
Note: You can change the "+" to "*" if you want to allow a leading or trailing dot.
Related
I am looking for regex which can help me replace strings like
source=abc/task=cde/env=it --> source='abc'/task='cde'/env='it'
To be more precise, I want to replace a string which starts with = and ends with either / or end of the string with ''
Tried code like this
"source=abc/task=cde/env=it".replaceAll("=(.*?)/","'$1'")
But that results in
source'abc'task'cde'env=it
Using lookahead and look behind:
(?<==)([^/]*)((?=/)|$)
Lookbehind allows you to specify what comes before your match. In this case an equals: (?<==).
The main match in my regex looks for any non-slash character, zero or more times: ([^/]*)
Lookahead allows you to specify what comes after your match. In this case, a slash: (?=/).
The $ matches the end of the line, so that the last item in your test data becomes quoted. ((?=/)|$) combines with this with the lookahead, meaning "either a slash comes after the match or this is the end of the line".
Here it is in action in a test.
#Test
public void test_quote_items() {
String regex = "(?<==)([^/]*)((?=/)|$)";
String actual = "source=abc/task=cde/env=it".replaceAll(regex,"'$1'");
String expected = "source='abc'/task='cde'/env='it'";
assertEquals(expected, actual);
}
Try
String input = "source=abc/task=cde/env=it".replaceAll("=(.*?)(/|$)","='$1'/");
The problems I found are that you are not replacing the =
and also the / is not there for the end of String, that also needs to be replaced when found.
output
source='abc'/task='cde'/env='it'/
If you don't want the last '/', that is trivial to remove isn't it.
We have a String as below.
\config\test\[name="sample"]\identifier["2"]\age["3"]
I need to remove the quotes surrounding the numbers. For example, the above string after replacement should look like below.
\config\test\[name="sample"]\identifier[2]\age[3]
Currently I'm trying with the regex as below
String.replaceAll("\"\\\\d\"", "");
This is replacing the numbers also. Please help to find out a regex for this.
You can use replaceAll with this regex \"(\d+)\" so you can replace the matching of \"(\d+)\" with the capturing group (\d+) :
String str = "\\config\\test\\[name=\"sample\"]\\identifier[\"2\"]\\age[\"3\"]";
str = str.replaceAll("\"(\\d+)\"", "$1");
//----------------------^____^------^^
Output
\config\test\[name="sample"]\identifier[2]\age[3]
regex demo
Take a look about Capturing Groups
We can try doing a blanket replacement of the following pattern:
\["(\d+)"\]
And replacing it with this:
\[$1\]
Note that we specifically target quoted numbers only appearing in square brackets. This minimizes the risk of accidentally doing an unintended replacement.
Code:
String input = "\\config\\test\\[name=\"sample\"]\\identifier[\"2\"]\\age[\"3\"]";
input = input.replaceAll("\\[\"(\\d+)\"\\]", "[$1]");
System.out.println(input);
Output:
\config\test\[name="sample"]\identifier[2]\age[3]
Demo here:
Rextester
You can use:
(?:"(?=\d)|(?<=\d)")
and replace it with nothing == ( "" )
fast test:
echo '\config\test\[name="sample"]\identifier["2"]\age["3"]' | perl -lpe 's/(?:"(?=\d)|(?<=\d)")//g'
the output:
\config\test\[name="sample"]\identifier[2]\age[3]
test2:
echo 'identifier["123"]\age["456"]' | perl -lpe 's/(?:"(?=\d)|(?<=\d)")//g'
the output:
identifier[123]\age[456]
NOTE
if you have only a single double quote " it works fine; otherwise you should add quantifier + for both beginning and end "
test3:
echo '"""""1234234"""""' | perl -lpe 's/(?:"+(?=\d)|(?<=\d)"+)//g'
the output:
1234234
Please help me out to get the specific regex to remove comma after a word pattern in java.
Assume, I would like to delete comma after each pattern where the pattern is <Word$TAG>, <Word$TAG>, <Word$TAG>, <Word$TAG>, <Word$TAG> now I want my output to be <Word$TAG> <Word$TAG> <Word$TAG> <Word$TAG> . if I used .replaceAll(), it will replace all commas, but in my <Word$TAG> Word may have a comma(,).
For example, Input.txt is as follows
mms§NNP_ACRON, site§N_NN, pe§PSP, ,,,,,§RD_PUNC, link§N_NN, ....§RD_PUNC, CID§NNP_ACRON, team§N_NN, :)§E
and Output.txt
mms§NNP_ACRON site§N_NN pe§PSP ,,,,,§RD_PUNC link§N_NN ....§RD_PUNC CID§NNP_ACRON team§N_NN :)§E
You could use ", " as search and replace it with " " (space) as below:
one.replace(", ", " ");
If you think, you have "myString, ,,," or multiple spaces in between, then you could use replace all with regex like
one.replaceAll(",\\s+", " ");
(?<=[^,\s]),
Try this.Replace by empty string.See demo.
http://regex101.com/r/lZ5mN8/5
Match the data you want, not the one you don't want.
You probably want ([^ ]+), and keep the bracketed data, separated by whitespace.
You might even want to narrow it down to ([^ ]+§[^ ]+),. Usually, stricter is better.
You could use a positive lookahead assertion to match all the commas which are followed by a space or end of the line anchor.
String s = "mms§NNP_ACRON, site§N_NN, pe§PSP, ,,,,,§RD_PUNC, link§N_NN, ....§RD_PUNC, CID§NNP_ACRON, team§N_NN, :)§E";
System.out.println(s.replaceAll(",(?=\\s|$)",""));
Output:
mms§NNP_ACRON site§N_NN pe§PSP ,,,,,§RD_PUNC link§N_NN ....§RD_PUNC CID§NNP_ACRON team§N_NN :)§E
I have this String: "Hello, my Name is [[Peter.java]]."
The desired split is: [Hello, my, Name, is, [[Peter.java]]]
I split at punktuation marks but completly ignore things in these brackets.
I tried:
string.split("(?!\\[\\[.*\\]\\])\\s*(\\,|\\.|\\s)\\s*")
but this doesnt work because the output is [Hello, my, Name, is, [[Peter, java]]]. Can you help me?
Other examples:
"Hello. My name is [[Peter.java]]" --> [Hello, My, name, is, [[Peter.java]]]
"Hi. How, [[are,you]]" --> [Hi, How, [[are,you]]]
You can use this regex to split:
[.,\s]+(?!\w+])
Working demo
The code:
public void testRegex() {
String str = "Hello. my Name is [[Peter.java]].";
String[] arr = str.split("[.,\\s]+(?!\\w+])");
System.out.println(Arrays.toString(arr));
}
// Output: [Hello, my, Name, is, [[Peter.java]]]
Edit: as HamZa pointed in his comment, the regex above fails is the string is something, like this]. So, to leverage the usage of SKIP & FAIL pcre feature, this regex can be improved by using:
\[\[.*?\]\] # Match our brackets
(*SKIP)(*FAIL) # Skip that match and proceed further
| # or
[\s.,]+ # any character of: whitespace (\n, \r, \t,
\f, and " "), '.', ',' (1 or more times)
Working demo
Instead of using String.split, you'll probably want to use a different sort of regex.
/\[\[(.*?)\]\]|(\w+)\W/g
Online demo
Then use a matcher to iterate through the matches.
I cant find a solution to this simple problem.
I want to replace two consecutive '' or `` by ".
Input:
some ``text'' dspsdj
Out:
some "text"
Why:
s.replaceAll("[`{2}'{2}]", "\"")
Out:
some ""text""
???
Thank you
You should do it like this:
s.replaceAll("``|''", "\"")
What you may have intended to do was this here:
s.replaceAll("[`']{2}", "\"")
But that wouldn't be entirely correct
String input = "some ``text'' dspsdj";
String output = input.replaceAll("`{2}|'{2}", "\"");
Put the cardinality after the class:
.replaceAll("[`']{2}", "\""));
Try this:
String resultString = subjectString.replaceAll("([\"'`])\\1", "\"");
Explanation:
<!--
(["'`])\1
Match the regular expression below and capture its match into backreference number 1 «(["'`])»
Match a single character present in the list “"'`” «["'`]»
Match the same text as most recently matched by capturing group number 1 «\1»
-->