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)");
Related
I have the following string: http://localhost:somePort/abc/soap/1.0
I want the string to just look like this: http://localhost:somePort/abc.
I want to use string.replaceAll but can't seem to get the regex right. My code looks like this: someString.replaceAll(".*\\babc\\b.*", "abc");
I'm wondering what I'm missing? I don't want to split the string or use .replaceFirst, as many solutions suggest.
It would seem to make more sense to use substring, but if you must use replaceAll, here's a way to do it.
You want to replace /abc and everything after it with just /abc.
string = string.replaceAll("/abc.*", "/abc")
If you want to be more discriminating you can include a word boundary after abc, giving you
string = string.replaceAll("/abc\\b.*", "/abc")
Just for explanation on the given regex, why it wont work:
\b \b - word boundaries are not required here and also as .* is added in the beginning it matches the whole string and when you try to replace it with "abc" it will replace the entire match with "abc". Hence you get the wrong answer. Instead, only try to match what is required and then whatever is matched that will be replaced with "abc" string.
someString.replaceAll("/abc.*", "/abc");
/abc.* - Looks specifically for /abc followed by 0 or more characters
/abc - Replaces the above match with /abc
You should use replaceFirst since after first match you are removing all after
text= text.replaceFirst("/abc.*", "/abc");
Or
You can use indexOf to get the index of certain word and then get substring.
String findWord = "abc";
text = text.substring(0, text.indexOf(findWord) + findWord.length());
I have 2 nested HashMaps as a String which I am trying to parse.
My String is as follows :
"20:[cost:431.14, Count:19, Tax:86.228"
Therefore I need to Split by ":[" in order to get my key, 20, For some reason I'm not able to do this.
I have tried :
myString.split(":[") and myString.split("\\:[") but neither seem to work.
Can anyone detect what I have wrong here?
Thanks in Advance
You have to escape the character [ , but not the character : like below:
String str = "20:[cost:431.14, Count:19, Tax:86.228";
String[] spl = str.split(":\\[");
String.split use regex.
Splits this string around matches of the given regular expression.
You need to escape [ since this is a "reserved" character in regular expresionn, not :
myString.split(":\\[")
Not that you could/should set a limit if you only want the first cell
myString.split(":\\[", 2);
This will return an array of 2 cell, so after the first occurence, it doesn't need to read the rest of the String. (This is not really necessary but good to know).
Use Pattern.quote to automatically escape your string
String string = "20:[cost:431.14, Count:19, Tax:86.228";
String[] split = string.split(Pattern.quote(":["));
Another solution :
Therefore I need to Split by ":[" in order to get my key, 20. For
some reason I'm not able to do this.
In this case you can use replaceAll with some regex to get this input so you can use :
String str = "20:[cost:431.14, :[Count:19, Tax:86.228";
String result = str.replaceAll("(.*?):\\[.*", "$1");// output 20
regex demo
If the key is just an integer you can use (\d+):\[ check regex demo
be noted '[' character is special character in regular expression so you have to make an escape character like \\ str.split(":\\["); and remember the string is immutable so if do you want to use it twice you have to reassign it with split like this String[] spl =str.split(":\\[");
Another solution if you just need the key "20" in your String is to substring it to get the part before the delimiter.
String key = myString.substring(0, myString.indexOf(":["));
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]","");
I have a following line : |A|B|C|D|100|E|GEN|smsplus|11|11|11| and I want to remove the first and last occurrence of |.
I have to check first if the | is available at first and last
I am sure that this will bring me some down votes but really don't know how to go about.
EDIT
I also have to check for the following :
A|B|C|D|100|E|GEN|smsplus|11|11|11| and |A|B|C|D|100|E|GEN|smsplus|11|11|11 and there is no
possibilities for || that is why I did not want to give any explanation for the same.
thanks for your valuable time.
You can perhaps use the regex:
^\\|(.*)\\|$
And replace by $1:
ideone demo
If the start and end of the string are not both |, there'll be no replacement.
EDIT: As per your last comment, if you want to remove the | if it's either at the start and/or at the end of the string, then use an OR operator (there's too many | in that question I think =P):
^\\||\\|$
Replace by nothing (empty string "")
EDIT2:
If the first regex is closer to what you need but also want to remove multiple pipes, add quantifiers:
^\\|+(.*)\\|+$
If the second regex (in first edit) is what you need but also want to remove multiple pipes, add quantifiers again!
^\\|+|\\|+$
Note that the replacement strings remain the same in both cases
Your comments lead me to believe that you want to do this:
String result = subject.replaceAll("^\\||\\|$", "");
This will change each of the following strings:
|A|B|C|
|A|B|C
A|B|C|
A|B|C
to the same string A|B|C.
See it on regex101.com.
Use indexOf() and lastIndexOf() from java.lang.String package
String s="|A|B|C|D|100|E|GEN|smsplus|11|11|11|";
s=s.substring(s.indexOf("|"),s.lastIndexOf("|"));
System.out.println(s); //Prints A|B|C|D|100|E|GEN|smsplus|11|11|11
Try,
String input = "|A|B|C|D|100|E|GEN|smsplus|11|11|11|";
input = input.substring(1, input.length()-1);
or use StringUtils.removeStart, removeEnd
StringUtils.removeStart(input, "|");
StringUtils.removeEnd(input, "|");
or
StringUtils.strip(input, "|");
I am new to regex. I am looking for regular expression which matches following pattern and extract the string,
key1=test1
key2="test1" // which will extract test1 stripping quotes
key3=New test
key4=New" "test // which will extract New" "test - as it is if the quotes come in between
I tried with \\s*(\\S+)\\s*=\\s*(\\S+*+) , but not sure how to include quotes if present. Any help will be really appreciated.
Here's a solution without regex to avoid problems with nested quotes:
String extractValue(String input) {
// check if '=' is present here...
String[] pair = input.split("=", 2);
String value = pair[1];
if (value.startsWith("\"") && value.endsWith("\"")) {
return value.substring(1, value.length() - 1);
}
return value;
}
Basically this is not without regex, because of the use of split(), but it's not using regex the way you were planning to use it.
A simple solution would be to just load it as a Properties, which will do exactly the parsing you're looking for. Otherwise, just read each line and split the string at the first "=".
You could use ^([^=]+)=("([^"]*)"|([^"].*))$, but the answer will show up in the third or fourth group depending on if the value was quoted or not so you'd need to check both and pull whichever one was not null.
For Regex, if you want to include " in your regex, simply escape it using \\". Whatever you are trying to achieve, test directly first at http://www.regexpal.com/