Regex captuing group - java

I am trying to capture host address from string with regex. My code looks like the following:
private static final Pattern OBTAIN_HOST_PATTERN = Pattern.compile("Host:\\s?(.*)");
public static String getHostAddress(String line) {
Matcher m = OBTAIN_HOST_PATTERN.matcher(line);
if (m.matches()) {
return OBTAIN_HOST_PATTERN.matcher(line).group(1);
}
return "Pattern does not match.";
}
Then I call getHostAddress("Host: abc"); and it gives me java.lang.IllegalStateException: No match found which means it matches the pattern but group capturing does not work. So, could you please help me find out why does this happen and what I am missing. Thanks in advance :)
Edit: I resolved the issue. It was because I am getting the matcher twice (or at least I think this was the reason), but can someone explain why does this happen?

The statement
return OBTAIN_HOST_PATTERN.matcher(line).group(1);
calls neither matches or find. As the if statement has already found a match so you can just do
return m.group(1);

You could even do better, by naming your group so you don't get confused with group indexes while trying to find your corresponding group. It can be achieved by doing the following thing :
"Host:\\s?(?<mygroupname>.*)"
and then
m.group("mygroupname")
A bit of doc about it : https://blogs.oracle.com/xuemingshen/entry/named_capturing_group_in_jdk7

Related

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 regular expression partial replace

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.

Partial Regex Extraction from URL

I had asked a question , many thanks for all the help.
I have a URL Like this.
/Hello/World/special/Case/2016/07/01/offer-015155.html
I need only "2016/07/01/offer-015155" this part and this dynamically changes each time. Could you help?
I tried "(.*?)" , "\d{4}/\d\d/\d\d/offer-\d+." but did not help.
When I run it says, not found . :(
If you want extract the part of an URL it will be quite enough to use something very simple like (.+)
Demo:
Example Regular Expression configuration (mind "Field to Check" bit)
References:
JMeter Regular Expressions
Perl 5 Regex Cheat sheet
Using RegEx (Regular Expression Extractor) With JMeter
You could consider using a positive lookbehind combined with a positive lookahead, like this:
(?<=\/Hello\/World\/special\/Case\/).*(?=.html)
This regex is very explicit but does the job. See this for an explanation of the regex: https://regex101.com/r/vY5mS2/2
EDIT:
Or simply use capture groups (https://regex101.com/r/vY5mS2/5)
\/Hello\/World\/special\/Case\/(.*)\.html
You can try the following code also:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("(\\d{4}/\\d{2}/\\d{2}/offer-\\d+)");
String testString = "/Hello/World/special/Case/2016/07/01/offer-015155.html";
Matcher matcher = pattern.matcher(testString);
if (matcher.find()) {
System.out.println(matcher.group());
}
}
}
Execution result: 2016/07/01/offer-015155

Regex and RSS Feed

i write a simple Rss Feed reader now i have this problem, in the item description i have the text but this text have this caracters <br/>
for example
"my dog <br/> is black and he <br/> stay on table "
now i wont to clear the string from this caracters, i wirte this metod
private static String IsMatch(String s, String pattern) {
try {
Pattern patt = Pattern.compile(pattern);
Matcher matcher = patt.matcher(s);
return matcher.group();
} catch (RuntimeException e) {
return "Error";
} }
and
String regex ="[<br/>]";
theString2=IsMatch(theString,regex);
AppLog.logString(theString2);
but But this method return always Error. Can any one tell me what's the problem?
best regads
Antonio
The problem is that you never invoke find() on your matcher. You must invoke find() before invoking group() (and test that find() returns true).
I am not sure of what your method IsMatch is supposed to do. As it is, it will either return the match (ie, "<br/>", assuming you invoke find() before) either return "Error".
Also, don't put the brackets around <br/> in your regexp, they are not needed.
I wouls really consider using replace instead of regexp for your purposes:
String s = "my dog <br/> is black and he <br/> stay on table ".replace("<br/>","");
As a recommendation, don't catch an exception without logging the it. They provide valuable information that should not be hidden. It make debug really hard when a problem occurs.
String regex ="(\<br\/\>)";
I think you need to pay attention to the grammar details about the regular expression:
1. escape the "<" like special characters
different language of regular expression have different specifications

Capturing dot and comma in Java RegExp

I have following code in Java:
Pattern fieldsPattern = Pattern.compile("(\"([^\"]+)\")|"
+"("+this.field_tag+"([0-9a-zA-Z_]+))");
Matcher fieldsMatcher = fieldsPattern.matcher(field);
while(fieldsMatcher.find())
{
//...
}
This code should capture expressions like "expression" and :expression (field_tag is just ":"). The problem occurs when I try to capture an expression like: "10.1" or "10,1". It dosen't work.
But expressions:
"10-1",
"10+1"
works as expected.
I also tried use this regexp on regexpal.com - site with javascript implementation of RegExp. On this site expressions like "10.1" and "10,1" works fine.
Is there any difference in java vs javascript in capturing dots? What am I doing wrong?
This works for me
Pattern fieldsPattern = Pattern.compile("(\"[^\"]+\")");
String field =" aa \"10\" \"10.1\" and \"10,1\"";
Matcher fieldsMatcher = fieldsPattern.matcher(field);
while(fieldsMatcher.find()) {
System.out.println(fieldsMatcher.group());
}
prints
"10"
"10.1"
"10,1"
The second set of brackets in the regex appear to be redundant, but are harmless.

Categories