How to fix the following RegEx? - java

I've the following piece of code. I use the Case-insensitve pattern modifier so it will find any occurrence, but what I want is the replacement to be exactly the chars that matched the pattern, keeping the case. How could I fix this?
String str = "Ten tender tEens";
String substr = "te";
str = str.replaceAll("(?i)"+substr, "("+substr+")");
System.out.println( str );
Desired output:
(Te)n (te)nder (tE)ens
Received output:
(te)n (te)nder (te)ens

replaceAll() work as the same ways as matcher(string).replaceAll(exp):
To make this work and for better understanding you can break the code like :
String str = "Ten tender tEens";
Pattern pattern=Pattern.compile("(?i)(te)");
Matcher matcher=pattern.matcher(str);
System.out.println( matcher.replaceAll("$1"));
Combining these steps you can use (does the same):
String substr = "te";
str = str.replaceAll("(?i)("+substr+")", "($1)");

You have to use
str = str.replaceAll("(?i)("+substr+"?)", "($1)");
This will create a group and replace the group.

You need to use capturing group.
str = str.replaceAll("(?i)("+substr+")", "($1)");

Related

How to split a string in between square brackets like below

I want to split the string
[{"starDate":"","endDate":"","relativeDays":,"cronExpression":""},{"starDate":"","endDate":"","relativeDays":,"cronExpression":""}]
to
{"starDate":"","endDate":"","relativeDays":,"cronExpression":""}
{"starDate":"","endDate":"","relativeDays":,"cronExpression":""}
Maybe this one-liner work out for you, since it's not clear whether you want two strings or one string with explicit line breaks
String str = "[{\"starDate\":\"\",\"endDate\":\"\",\"relativeDays\":,\"cronExpression\":\"\"},{\"starDate\":\"\",\"endDate\":\"\",\"relativeDays\":,\"cronExpression\":\"\"}]";
str = String.join("},", str.replaceAll("^\\[|]$+", "").split("},"));
You should ideally do this the proper way using JSON parsing if it applies.
Meanwhile, for this very specific case, you can use a Regular Expression to extract the parts you need (Example in Java) :
String input = "[{\"starDate\":\"\",\"endDate\":\"\",\"relativeDays\":,\"cronExpression\":\"\"},{\"starDate\":\"\",\"endDate\":\"\",\"relativeDays\":,\"cronExpression\":\"\"}]";
Matcher matcher = Pattern.compile("(\\{[^\\}]*\\})").matcher(input);
while( matcher.find() )
System.out.println(matcher.group(1));
This will result in :
{"starDate":"","endDate":"","relativeDays":,"cronExpression":""}
{"starDate":"","endDate":"","relativeDays":,"cronExpression":""}

Regex to convert this String

How can I convert this String AB23-01-0001 to AB23010001( replacing the "-" with "") and AB230001 (removing the middle part) using regex in Java, right row I'm using replace for the first case and substring and appending them into a SB for the second case. Just wanted to know how to achieve it using REGEX.
Why not use the method built into the String class?
String newString = "AB23-01-0001".replaceAll("[-]", "");
Note the use of [] - a regex string, since you are just replacing a -, you can omit them.
str = "AB23-01-0001"
happy = str.replaceAll("[^a-zA-Z0-9]", "");
from https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)
Try this:
Pattern p1 = Pattern.compile("-[^-]*-");
Matcher m1 = p1.matcher("AB23-01-0001");
System.out.println(m1.replaceAll(""));
To test it
A replacement to the patter is Pattern.compile("-[\\d\\w]+-")
You can use build-in method to remove both parts at once with RegEx:
String value = "AB23-01-0001";
value = value.replaceAll("-[\\d\\w]+-", "");

How to replace all matching patterns only if it does not match another pattern?

I want to replace a substring that matches a pattern, only if it does not match a different pattern. For example, in the code shown below, I want to replace all '%s' but leave ':%s' untouched.
String template1 = "Hello:%s";
String template2 = "Hello%s";
String regex = "[%s&&^[:%s]]";
String str = template1.replaceAll(regex, "");
System.out.println(str);
str = template2.replaceAll(regex, "");
System.out.println(str);
The output should be:
Hello:%s
Hello
I am missing something in my regex. Any clues? Thanks!
Use a negative lookbehind to achieve your goal:
String regex = "(?<!:)%s";
It matches %s only if there is not a : right before it.

parsing string with substring

what is the best way to parse string
Example
SomeName_Some1_Name2_SomeName3
I want to get out SomeName. What is the best way to do? With substring and calculationg positions or is another better way
You can match pattern SomeName for extracting-
String str= "SomeName_Some1_Name2_SomeName3";
Pattern ptrn= Pattern.compile("SomeName");
Matcher matcher = ptrn.matcher(str);
while(matcher.find()){
System.out.println(matcher.group());
}
Split it by underscore _ using method split()
Get index # 0 from returning array from previous step
If you know the delimiter then you can just try this:
System.out.println("SomeName_Some1_Name2_SomeName3".split("_")[0]);
See also: Javadoc of String.split()
Depends on your configuration and whether you're interested in the other fields.
In that case, go for splitting the string using the _ separator.
In case you just want a part of the string, I'ld just go for substringing in combination with indexOf('_').
In case you want all Occurences you could also find all occurences of 'someName' in your text.
Use regex and Pattern Matcher API to get SomeName.
Here you go:
String str = "SomeName_Some1_Name2_SomeName3";
String newStr = str.substring(0, str.indexOf("_"));
System.out.println(newStr);
Output:
SomeName
String your_String = "SomeName_Some1_Name2_SomeName3";
your_String = your_String.split("_")[0];
Log.v("log","your string "+ your_String);
String str = "SomeName_Some1_Name2_SomeName3";
String output = str.split ( "_" ) [ 0 ];
you will get your output as SomeName.

regular expression to split the string in java

I want to split the string say [AO_12345678, Real Estate] into AO_12345678 and Real Estate
how can I do this in Java using regex?
main issue m facing is in avoiding "[" and "]"
please help
Does it really have to be regex?
if not:
String s = "[AO_12345678, Real Estate]";
String[] split = s.substring(1, s.length()-1).split(", ");
I'd go the pragmatic way:
String org = "[AO_12345678, Real Estate]";
String plain = null;
if(org.startsWith("[") {
if(org.endsWith("]") {
plain = org.subString(1, org.length());
} else {
plain = org.subString(1, org.length() + 1);
}
}
String[] result = org.split(",");
If the string is always surrounded with '[]' you can just substring it without checking.
One easy way, assuming the format of all your inputs is consistent, is to ignore regex altogether and just split it. Something like the following would work:
String[] parts = input.split(","); // parts is ["[AO_12345678", "Real Estate]"]
String firstWithoutBrace = parts[0].substring(1);
String secondWithoutBrace = parts[1].substring(0, parts[1].length() - 1);
String first = firstWithoutBrace.trim();
String second = secondWithoutBrace.trim();
Of course you can tailor this as you wish - you might want to check whether the braces are present before removing them, for example. Or you might want to keep any spaces before the comma as part of the first string. This should give you a basis to modify to your specific requirements however.
And in a simple case like this I'd much prefer code like the above to a regex that extracted the two strings - I consider the former much clearer!
you can also use StringTokenizer. Here is the code:
String str="[AO_12345678, Real Estate]"
StringTokenizer st=new StringTokenizer(str,"[],",false);
String s1 = st.nextToken();
String s2 = st.nextToken();
s1=AO_12345678
s1=Real Estate
Refer to javadocs for reading about StringTokenizer
http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html
Another option using regular expressions (RE) capturing groups:
private static void extract(String text) {
Pattern pattern = Pattern.compile("\\[(.*),\\s*(.*)\\]");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) { // or .matches for matching the whole text
String id = matcher.group(1);
String name = matcher.group(2);
// do something with id and name
System.out.printf("ID: %s%nName: %s%n", id, name);
}
}
If speed/memory is a concern, the RE can be optimized to (using Possessive quantifiers instead of Greedy ones)
"\\[([^,]*+),\\s*+([^\\]]*+)\\]"

Categories