java regular expression partial replace - java

I am working with some legacy code that has a static method call which we need to remove from our source tree.
The existing code is as follows:
Logger.getInstance(JdkUtil.forceInit(SomeBusiness.class));
What we need to end up with is:
Logger.getInstance(SomeBusiness.class);
I've spent all day today trying to figure out how to do that replacement. Since I have very little experience with regular expressions, I have only been able to come up with a pattern that matches the source string.
The pattern JdkUtil.forceInit([a-zA-Z_0-9]*.class) finds matches on the input string I am providing. I've tested this at https://www.freeformatter.com/java-regex-tester.html
So if anyone can post a Java solution to this, I would really appreciate it.
Below is some Groovy code that I have so far. What I am missing is to how correctly replacement explained above.
String source = 'Logger.getInstance(JdkUtil.forceInit(RtpRuleEngineCompiledImpl.class))'
String regexpPattern = 'JdkUtil.forceInit\\([a-zA-Z_0-9\\)]*.class\\)'
String replaced = source.replaceFirst(regexpPattern, 'hello')
println replaced
When I run the above code I get the following output:
Logger.getInstance(hello)
Obviously 'hello' is just for testing.
Thanks in advance to anyone who can give me some suggestions.

You'll likely want to do something such as:
class StackOverflow {
public static void main(String[] args) {
String source = "Logger.getInstance(JdkUtil.forceInit(RtpRuleEngineCompiledImpl.class))";
String regexpPattern = "JdkUtil.forceInit\\(([a-zA-Z_0-9]*.class)\\)";
String replaced = source.replaceFirst(regexpPattern, "$1");
System.out.println(replaced);
}
}
Result:
Logger.getInstance(RtpRuleEngineCompiledImpl.class)
The capture group ($1) replaces the entire string which was within the parentheses.

Related

Java regular Expression End of String

I want to replace some part of a string, but only if the part is the last part of the string.
Example:
public class Test {
public static void banana(){
String test = "I love Banana";
System.out.println("Test: "+test+"\nRegEx: "+test.replace("a$","as"));
}
}
The code above does not change anything. Without the $-sign I get I love Basnasnas, which is not, what I want. I tried escape the dollar with a backslash, didn't work out. Where is my mistake?
Found the problem, I have to use replaceAll instead of replace! Thank you!

Need to get Particular word using java Regex

I want to get one particular word using regex in java. thanks
in the below paragraph, I need to find the network interface name
resource "azurerm_network_interface" "nic_LinuxVMCent-nhi" {
name = "nic_LinuxVMCent-nhi"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
ip_configuration {
name = "pubIP_LinuxVMCent-nhi"
subnet_id = azurerm_subnet.sub_wind12VM-PtN.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.pubIP_LinuxVMCent-nhi.id
}
}
data "azurerm_snapshot" "snapLinuxVMCent-nhi" {
name = "CentOS76New-0"
resource_group_name = "SaaSworkloadsnaps"
}
Expected Result Ex:
nic_LinuxVMCent-nhi
This is a multi-line bit of text. However, there appears to be a line which you could recognise with a regex:
resource "azurerm_network_interface" "nic_LinuxVMCent-nhi" {
So the regex for that would be ^resource "azurerm_network_interface" "([^"]+)" {$ - see https://regexr.com/67ldb
You can use Matcher.match to see if the any line matches this expression and if it does then matcher.group(1) will be the value you're looking for.
you can use this regex to find the network interface name:
(?<=resource \"azurerm_network_interface\" \").+(?=\" {)
I have used lookahead to find the name.
Also, here's a link to regex101:
Link
I don't know network interfaces so,
This regex solution is specific to "azurerm_network_interface."
If you need any additional help, please comment down below.
Cheers :)

Java matcher pattern

I have a list of strings: ab10sdj, ba1wqa, cd03asce, dfasc, etc. I'm looking to get the group of digits from the strings that start either with ab, or with ba.
So if a string starts with ab/ba, I need the group of digits right after them. If there a way for me to achieve this via a java matcher/regex?
Apologies for posting this as an answer but my current reputation level doesn't allow me 'comment' ... yet :)
As apposed to simply giving an answer to a relatively simple problem (without insulting your intelligence), I can help with diagnosing a solution. Try thinking the problem through in your mind, step by step. That is:
How do you test for the first 2 characters in a string?
If the test to point 1 passes (IE, they are ab or ba), how do you then process the rest of the string to test for 'digits' only?
How do you stop processing the 'digits' when you reach a non-digit?
Once you have your ab/ba prefix, only extracted the 'digits' immediately following the test condition, how will you handle the digits extracted?
Before considering placing the digits in a primitive type, you may wish to consider how many digits one may expect?
All the best with your code!
You can try regex like this :
public static void main(String[] args) {
// ab10sdj, ba1wqa, cd03asce, dfasc
String s1 = "ab10sdj";
String s2 = "ba1wqa";
String s3 = "cd03asce";
String s4 = "dfasc";
String pattern = "^(ab|ba)(\\d+).*";
System.out.println(s1.replaceAll(pattern, "$2")); // output 10
System.out.println(s2.replaceAll(pattern, "$2")); // output 1
System.out.println(s3.replaceAll(pattern, "$2")); // output cd03asce i.e, no change
System.out.println(s4.replaceAll(pattern, "$2")); // output dfasc i.e, no change
}

Java regex to strip out XML tags, but not tag contents

I have the following Java code:
str = str.replaceAll("<.*?>.*?</.*?>|<.*?/>", "");
This turns a String like so:
How now <fizz>brown</fizz> cow.
Into:
How now cow.
However, I want it to just strip the <fizz> and </fizz> tags, or just standalone </fizz> tags, and leave the element's content alone. So, a regex that would turn the above into:
How now brown cow.
Or, using a more complex String, somethng that turns:
How <buzz>now <fizz>brown</fizz><yoda/></buzz> cow.
Into:
How now brown cow.
I tried this:
str = str.replaceAll("<.*?></.*?>|<.*?/>", "");
And that doesn't work at all. Any ideas? Thanks in advance!
"How now <fizz>brown</fizz> cow.".replaceAll("<[^>]+>", "")
You were almost there ;)
Try this:
str = str.replaceAll("<.*?>", "")
While there are other correct answers, none give any explanation.
The reason your regex <.*?>.*?</.*?>|<.*?/> doesn't work is because it will select any tags as well as everything inside them. You can see that in action on debuggex.
The reason your second attempt <.*?></.*?>|<.*?/> doesn't work is because it will select from the beginning of a tag up to the first close tag following a tag. That is kind of a mouthful, but you can understand better what's going on in this example.
The regex you need is much simpler: <.*?>. It simply selects every tag, ignoring if it's open/close. Visualization.
You can try this too:
str = str.replaceAll("<.*?>", "");
Please have a look at the below example for better understanding:
public class StringUtils {
public static void main(String[] args) {
System.out.println(StringUtils.replaceAll("How now <fizz>brown</fizz> cow."));
System.out.println(StringUtils.replaceAll("How <buzz>now <fizz>brown</fizz><yoda/></buzz> cow."));
}
public static String replaceAll(String strInput) {
return strInput.replaceAll("<.*?>", "");
}
}
Output:
How now brown cow.
How now brown cow.
This isn't elegant, but it is easy to follow. The below code removes the start and end XML tags if they are present in a line together
<url>"www.xml.com"<\url> , <body>"This is xml"<\body>
Regex :
to_replace='<\w*>|<\/\w*>',value=""
If you want to parse XML log file so you can do with regex {java}, <[^<]+<.so you get <name>DEV</name>. Output like name>DEV. You have to just play with REGEX.

Regex submitting with empty string

I have the following REGEX that I'm serving up to java via an xml file.
[a-zA-Z -\(\) \-]+
This regex is used to validate server side and client side (via javascript) and works pretty well at allowing only alphabetic content and a few other characters...
My problem is that it will also allow zero lenth strings / empty through.
Does anyone have a simple and yet elegant solution to this?
I already tried...
[a-zA-Z -\(\) \-]{1,}+
but that didn;t seem to work.
Cheers!
UPDATE FOLLOWING INVESTIGATION
It appears the code I provided does in fact work...
String inputStr = " ";
String pattern = "[a-zA-Z -\\(\\) \\-]+";
boolean patternMatched = java.util.regex.Pattern.matches(pattern, inputStr);
if ( patternMatched ){
out.println("Pattern MATCHED");
}else{
out.println("NOT MATCHED");
}
After looking at this more closely I think the problem may well be within the logic of some of my java bean coding... It appears the regex is dropped out at the point where the string parse should take place, thereby allowing empty strings to be submitted... And also any other string... EEJIT that I am...
Cheers for the help in peer reviewing my initial stupid though....!
Have you tried this:
[a-zA-Z -\(\) \-]+

Categories