Java regex pattern for a string - java

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/

Related

How to replace special character }} in a string with "" using regexp in java

I am trying to replace special character }} in a string with "" using regexp in Java, I tired the below two methods and it doesn't work. Please let me know what is wrong with these statements.
Note the string would also contain } which I would like to retain. Goal is to replace only }}.
Method 1:
String buffer = obj.toJSONString() + ",";
String result = buffer.replaceAll(Pattern.quote("(?<![\\w\\d])}}(?![\\w\\d])"), "");
Method 2:
Pattern.compile("(?<![\\w\\d])}}(?![\\w\\d])").matcher(buffer).replaceAll("");
The quote in the following:
String result = buffer.replaceAll(Pattern.quote("(?<![\\w\\d])}}(?![\\w\\d])"), "");
says to treat the regex as a literal string. That's wrong.
If you simply want to remove all }} irrespective of context:
String result = buffer.replaceAll(Pattern.quote("}}"), "");
If you do need to respect the context, don't Pattern.quote(...) the regex!
The other problem is in the way that you attempt to specify the character classes. Since \d is a subset of \w, it is unnecessary to combine them. Just do this instead:
String result = buffer.replaceAll("(?<!\\w)\\}\\}(?!\\w)"), "");
I'm not sure if it is strictly necessary to quote the } characters, but it is harmless if it is not necessary.
Dont' use Pattern.quote, use a literal regex pattern, and escape the brackets:
Stringbuffer = obj.toJSONString() + ",";
String result = buffer.replaceAll("(?<![\\w\\d])\\}\\}(?![\\w\\d])", "");
Using Pattern.quote tells the regex engine to treat the string as literal. This does mean the brackets would not have to be escaped, but it would also render your lookarounds as literal text, probably not what you have in mind.
The method 2 still needs to escape special characters }
Pattern.compile("(?<![\\w\\d])\\}\\}(?![\\w\\d])").matcher(buffer).replaceAll("");
Can you please try same with Apache StringUtils. It’s faster and should work in your case. Kindly find following links for reference.
apache-stringutils-vs-java-implementation-of-replace
Apache StringUtils 3.6

Unable to Split by ":[" in Java

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(":["));

Java regex for inserting text between braces

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)");

Validate string that contains template in regex

I have a problem trying to validate this string...
So, the user selects a template: q( ). Then, the user fills in the contents inside the brackets which can end up like this:
q(a,b,c)
I have tried different ways using regex to validate this String, but it keeps returning the answer "No". I believe the problem is "q(" and ")" in my regex as I am not sure how it should look like.
Here's a snippet of the code:
String data2 = "q(a,b,c)";
String regex2 = "q([a-zA-Z0-9,'])";
if(data2.matches(regex2)){
System.out.println("yes");
}
else{
System.out.println("No");
}
I do have an alternative way by removing "q(" and ")" in data2 string, but I rather have it in regex without the need of removing characters in a String.
Any suggestions?
You need to escape the parentheses (and escape the escape character so that it compiles) and add a + at the end to indicate one or more characters
String regex2 = "q\\([a-zA-Z0-9,']+\\)";
You can read the meaning of every character in a regular expression in the Pattern javadoc.

Why the second argument is not being taken as regex?

I came across an interesting question on java regex
Is there a regular expression way to replace a set of characters with another set (like shell tr command)?
So I tried the following:
String a = "abc";
a = a.replaceAll("[a-z]", "[A-Z]");
Now if I get print a the output is
[A-Z][A-Z][A-Z]
Here I think the compiler is taking the first argument as gegex, but not the second argument.
So is there any problem with this code or something else is the reason???
This is the way replaceAll works.
See API:
public String replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
The answer to the linked question is a quite clear »No«, so this should come as no surprise.
As you can see from the documentation the second argument is indeed a regular string that is used as replacement:
Parameters:
regex – the regular expression to which this string is to be matched
replacement – the string to be substituted for each match
second argument is simple String that will get substituted according to API
If you want to turn lower case to upper case, there is a toUpperCase function available in String class. For equivalent functionality to tr utility, I think there is no support in Java (up to Java 7).
The replacement string is usually take literally, except for the sequence $n where n denotes the number of the capturing group in the regex. This will use captured string from the match as replacement.
I consider regex as a way to express a condition (i.e does a given string match this expression). With that in mind, what you are asking would mean "please replace what matches in my string with ... another condition" which doesn't make much sens.
Now by trying to understand what you are looking for, it ssems to me that you want to find some automatic mapping between classes of characters (e.g. [a-z] -> [A-Z]). As far as I know this does not exist and you would have to write it yourself (except for the forementionned toUpperCase())
public String replaceAll(String regex, String replacement)
First argument is regular expression if substring matches with that pattern that will be replaced by second argument ,if you want to convert to lowercase to upper case use
toUpperCase()
method
You should look into jtr. Example of usage:
String hello = "abccdefgdhcij";
CharacterReplacer characterReplacer;
try {
characterReplacer = new CharacterReplacer("a-j", "Helo, Wrd!");
hello = characterReplacer.doReplacement(hello);
} catch(CharacterParseException e) {
}
System.out.println(hello);
Output:
Hello, World!

Categories