Java Pattern Regex Won't Match [duplicate] - java

This question already has answers here:
Rationale for Matcher throwing IllegalStateException when no 'matching' method is called
(6 answers)
Closed 7 years ago.
Can't figure out why the below pattern isn't matching for anything. Including some of my more simple test examples :
// Pattern attempts to match against a String containing ?[0-9]?+
Pattern groups = Pattern.compile(".*?\\?([2-9]+)\\?.*");
Matcher m = groups.matcher("incSkl(?2?,2)");
int val = Integer.parseInt(m.group(1));
Even went super simple and tried feeding in a simple input of "?2?" to matcher. Still will error out on line 3.
Strangely the regex tester below seems to agree with me. It says both inputs should be a valid full match, without any flags needed on the Pattern
http://www.regexplanet.com/advanced/java/index.html
What's going on here? I even threw something up on CoderPad just to make sure there wasn't something 'off' with my environment, and it errors with 'no match' as well.
I realize at this point I could probably do something with find() (and that option for this use case would be the most sensible), but I've never had anything like this happen, and at this point want to know why it can't do a full match when most other regex implementations go through with no problem.

You need to call:
m.find()
or
m.matches()
before calling: m.group(1) otherwise your code will throw a nice Exception at the time of calling group()

Related

regular expression for finding few strings in between one long string [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I am looking for regex pattern to extract the two words from a given text. I am looking for regex for applying in config file which would substitute in java code
For example - I have a long string - pabc_extracted_data_US_NETFLI_2020-11-26-17-02-03.csv.gz
I want to extract whatever string coming after pabc_extracted_data_ (here US) and also looking for another regex for whatever string coming after pabc_extracted_data_US_ (here NETFLI)..
so my expected output for two regex would be US & NETFLI. Please help me as I'm new to this regex world
edited:
first regex will be something like:
pabc_extracted_data_([^_]+)_
second will be something like:
pabc_extracted_data_[^_]+_([^_]+)
this seems to work:

regex see if lowercase exists [duplicate]

This question already has answers here:
Trying to check if string contains special characters or lowercase java
(4 answers)
Closed 7 years ago.
I'm still learning with regex.
I'm trying to check if the string contains ANY lowercase values. If it does, I just want to return false.
I've read answers on here, but they don't seem to work in my program, YET, they work in a regex emulator online.
if (str.matches("[a-z]+"){
System.out.println("removed");
return false;
This seems to highlight the lowercase letters in regexr but not in my program. Any help please?
If you're just looking for the correct regular expression itself .*[a-z].* as provided by #Adrian Leonhard as a comment above, is indeed correct. However, I think its important to mention that regular expressions take a very long time to compile, and if this if statement is nested in a loop it might be a good idea to use the full regular expression implementation in java.util.regex.*. rather than the convenience methods provided in String. To do so, first compile a Pattern object from your string.
Pattern p = Pattern.compile(".*[a-z].*");
This way the regex only has to be compiled once instead of every time String#matches(String regex) is called. Regex compilation is very computationally intensive. Then, create a matcher using your input string.
Matcher m = p.matcher(str);
Now, call Matcher#find()
if(m.find()) {
//Your code here
}
However, you could also just test to see if
str.toUpperCase().equals(str)
It's up to you. I would only use regex if absolutely necessary as it can slow down your program, and isn't very elegant in this case. At least you know how to use them properly in the future now.

digit regex no match

I keep getting an error that I shouldn't be getting and I am no regex expert but it should be so simple. I looked over it so many times and can't figure out why it isn't working. I have also searched a bunch for something similar but I can't find anyone that has the same problem.
This is the error I'm getting:
Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:485)
at DailyData.importUsers(DailyData.java:456)
at DailyData.main(DailyData.java:40)
Here is my code, through debugging I found its the last line that gives the error:
Pattern memberSincePattern = Pattern.compile("\\W*(\\d+):(\\d+):(\\d+)\\W*(\\d+)/(\\d+)/(\\d+)");
Matcher memberSinceMatcher = memberSincePattern.matcher("12:12:12 12/12/2012");
String msGroupOne = memberSinceMatcher.group(1);
I am using eclipse on Ubuntu 14.04 LTS.
I have imported the proper libraries and have tried \d{1,2} for the digits as well as getting rid of the leading \W*. I want it to be able to grab either 1 or two digits for each group.
I get no syntax errors or warnings on this either.
As the exception indicates, you need to find a match to your regex before looking for a matched group.
For example, you could use Matcher#matches, as follows:
Pattern memberSincePattern = Pattern.compile("\\W*(\\d+):(\\d+):(\\d+)\\W*(\\d+)/(\\d+)/(\\d+)");
Matcher memberSinceMatcher = memberSincePattern.matcher("12:12:12 12/12/2012");
if(memberSinceMatcher.matches()) {
String msGroupOne = memberSinceMatcher.group(1);
}
Here's the javadoc entry for Matcher#matches.
As a side note, I'd like to point out that if you want to match only a sub-sequence of your original String, at least one time, you should use Matcher#find instead of Matcher#matches. Possibly in a while loop :)

Simple Java regular expression matching fails

Before y'all jump on me for posting something similar to previous questions asked, yes, there seem to be a number of regex related questions but nothing which seems to help me, or at least that I can see.
I am trying to parse strings in JAVA using PATTERN and MATCHER and am really having no joy. My regular expression seems to match my input string when I use a few of the online regular expression testing websites but Java simply does not match my expression.
My input string is:
"Big apple" title="Little Apple" type="Container" url="http://malcolm.com/testing"
The regular expression I am using to match is ".*" title="(.*)" type="Container" url="(.*)"
Essentially I want to pull out the text within the second and the fourth set of quotes. There will always be 4 sets of quotes with text within and around.
I am coding as follows:
Variable XMLSubstring contains the string above (including the quotes) and is as stated, even when I print it out.
Pattern p = Pattern.compile(".* title=\"(.*)\" type=\"Container\" url=\"(.*)\"");
m = p.matcher(XMLSubstring);
It doesn't appear to be rocket science I'm attempting but I'm pulling my hair out trying to debug the bloody thing.
Is there something wrong with my regex pattern?
Is there something wrong with the code I am using?
Am I simply a moron and should stop coding with immediate effect?
EDIT & UPDATE: I have found the problem. My string had a space at the end of it which was breaking the parser! How silly, and I think based on that, I need to accept the third suggestion of mine and give up programming. Thanks all for your assistance.
Try this,
String str="\"Big apple\" title=\"Little Apple\" type=\"Container\" url=\"http://malcolm.com/testing\"";
Pattern p=Pattern.compile(".* title=\\\".*\\\" type=\\\"Container\\\" url=\\\".*\\\"");
Matcher m=p.matcher(str);

Trouble implementing partial matches with regular expression on Android

I am creating a regular expression to evaluate if an IP address is a valid multicast address. This validation is occurring in real time while you type (if you type an invalid / out of range character it is not accepted) so I cannot simply evaluate the end result against the regex. The problem I am having with it is that it allows for a double period after each group of numbers (224.. , 224.0.., 224.0.0.. all show as valid).
The code below is a static representation of what's happening. Somehow 224.. is showing as a legal value. I've tested this regex online (non-java'ized: ^2(2[4-9]|3\d)(.(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$ ) and it works perfectly and does not accept the invalid input i'm describing.
Pattern p = Pattern.compile("^2(2[4-9]|3\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");
Matcher m = p.matcher("224..");
if (!m.matches() && !m.hitEnd()) {
System.out.println("Invalid");
} else {
System.out.println("Valid");
}
It seems that the method m.hitEnd() is evaluating to true whenever I input 224.. which does not make sense to me.
If someone could please look this over and make sure I'm not making any obvious mistake and maybe explain why hitEnd() is returning true in this case I'd appreciate it.
Thanks everyone.
After doing some evaluating myself (after discovering this was on Android), I realized that the same code responds differently on Dalvik than it does on a regular JVM.
The code is:
Pattern p = Pattern.compile("^2(2[4-9]|3\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");
Matcher m = p.matcher("224..");
if (!m.matches() && !m.hitEnd()) {
System.out.println("Invalid");
} else {
System.out.println("Valid");
}
This code (albeit modified a bit), prints Valid on Android and Invalid on the JVM.
I do not know how have you tested your regex but it does not look correct according to your description.
Your regext requires all 4 sections of digits. There is no chance it will match 224..
Only [0-1] and \d are marked with question mark and therefore are optional.
So, without dealing with details of limitations of wich specific digits are permitted I'd suggest you something like this:
^\\d{1-3}\\.(\\d{0-3}\\.)?(\\d{0-3}\\.)?(\\d{0-3}\\.)?$
And you do not have to use hitEnd(): $ in the end is enough. And do not use matches(). Use find() instead. matches() is like find() but adds ^ and $ automatically.
I just tested out your code and m.hitEnd() evaluates to false for me, and I am receiving invalid...
So I'm not really sure what the problem here is?
I reported bug 20625 in Dalvik. In the interim, you don't need to use hitEnd(), having the $ suffix should be sufficient.
public void testHitEnd() {
String text = "b";
String pattern = "^aa$";
Matcher matcher = Pattern.compile(pattern).matcher(text);
assertFalse(matcher.matches());
assertFalse(matcher.hitEnd());
}

Categories