Java Regex not finding all results [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Using Java 8, I have the following snippets of code:
Here:
private ArrayList<MatchResult> allMatches(final Pattern p, final CharSequence input){
ArrayList<MatchResult> results = new ArrayList<MatchResult>();
Matcher matcher = p.matcher(input);
while(matcher.find()){
results.add(matcher.toMatchResult());
}
return results;
}
And the main method here:
List<MatchResult> matches = allMatches(Pattern.compile(searchString), searchable.getText());
System.out.println(matches.size());
The searchString variable holds the following String:
(?im)Exception
The searchable's text is this:
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (ORA-28040: No matching authentication protocol
As you can see, there are 3 times in the searchable's text where we see the word "exception". However, Java is only finding 2 of them.
The output of the code is this:
2
One more piece of information about this, is that the searchString variable comes from the user.
My question is regarding the number of matches. Java seems to be missing some matches when using regex. The text I provided comes from a bigger file, and this isn't the first one it's missed, but it is the one I'm focusing on to hopefully have some hint of where to look. What is going on here? Why is Java missing some matches? Is there a more accurate regular expression search that I should use?
To hopefully help those that aren't familiar with the "(?im)" in the searchString variable, I've located a resource here.

Found the problem, it was not in the code I posted, as that is proprietary code and cannot be put out in public domain. But basically the searchString was not being sent to the Pattern compiler correctly and the "(?im)" was stripped off before arrival. Thank you all for your help.

Related

Defining String with the char '{' in JAVA [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to define a Constant with some pattern identifier to be able to replace it in large String constructs later, for example in a complex Querie.
private static String SUBSTITUTE_1 = "${substitute1}";
But when I do that, compiler complains about expecting a number next to the '{' char.
My question is: Is there a way to escape the special characters in the string?
private static String SUBSTITUTE_1 = "\$\{substitute1\}";
This does not work.
This is strange because if I define a String builder like so
private static String SUBSTITUTE_1 = new StringBuilder("${substitute1}").toString();
there no problem with the special characters.
Unless there is something really obvious that i'm missing this does not make sense.
Thanks in advance.
Apparently, Intellij's compiler detects that somewhere in the code I'm using the defined constant in a String.replaceAll() instruction and complains about its definition as "${x}" is not a valid Regex Expression.
Solution is change to String.replace().

Regex that removes everything but the number [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to use java's string.replaceAll() or replaceFirst() method in order to edit data read from a pdf document. A line of data that could be returned is:
21/1**E (6-11) 4479 77000327633 (U)
I wish to only store the 77000327633 into a variable for working with and looking for the correct regex that will capture ONLY this 11 digit number. I've tried searching around for a regex but nothing seems to give me my desired outcome.
It could be done like this:
String value = "21/1**E (6-11) 4479 77000327633 (U)";
Pattern pattern = Pattern.compile(".* (\\d{11}) .*");
System.out.println(pattern.matcher(value).replaceAll("$1"));
Output:
77000327633
NB: This assumes that your number has 11 digits and that there is a space before and after.
NB2: It is not meant to be perfect it is only to show the idea which is here to define a global pattern with a group and replace everything by the content of the group
This is it : (.*)[ ]([0-9])*[ ](.*)
Can access to your value using $2

Filtering a string went wrong [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Consider we have the following set of String:
String text="Geeraertsb,∗,1";
String text2="bla bla";
String text3="in terms of &#1";
I would like to get those strings that do not contain either of & and/or "*". so that means only `text2" is of interest.
however if I use:
if(!text.contains("*"))
{
//do something
}
if(!text3.contains("&"))
{
//do something
}
it does enter the if statement while it should not. Any idea why?
You're using different characters
∗ is not equal to *
You may want to check that you're using the same set of characters for you String objects and your conditions.
You can try like this:
Pattern mypat = Pattern.compile("[^a-z ]", Pattern.CASE_INSENSITIVE);
Matcher mat = mypat.matcher("Geeraertsb,∗,1");
boolean found = mat.find();
if (found)
{
//code
}
The above code uses the regex approach in which you try to check if your string contains anything apart from the alphabets.

Parsing string that contains user entered free text [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to parse strings of the following format in Java:
Number-Action-Msg, Number-Action-Msg, Number-Action-Msg, Number-Action-Msg, ...
For example
"512-WARN-Cannot update the name.,615-PREVENT-The app is currently down, please try again later.,736-PREVENT-Testing,"
I would like to get an array with the following entries:
512-WARN-Cannot update the name.
615-PREVENT-The app is currently down, please try again later.
736-PREVENT-Testing
The problem is that the message is user entered, so I can't rely on just the commas to split up the String. The actions will always be WARN or PREVENT. What's the best way to accomplish this parsing? Thanks!
Instead of splitting by comma you can use this lookahead based regex for matching:
(\d+-(?:WARN|PREVENT).*?)(?=,\d+-(?:WARN|PREVENT)|,$)
RegEx Demo
(?=,\d+-(?:WARN|PREVENT)|,$) is a positive lookahead to assert there is a comma followed by digits-(WARN|PREVENT) or end of line ahead.
Seems quite simple:
Regular expression:
WARN|PREVENT
Debuggex Demo
In java:
String string = "512-WARN-Cannot update the name.,615-PREVENT-The app is currently down, please try again later.,736-PREVENT-Testing,";
String regex = "WARN|PREVENT";
System.out.println(Arrays.toString(string.split(regex)));
Will output:
[512-, -Cannot update the name.,615-, -The app is currently down, please try again later.,736-, -Testing,]
Of course you may want to adjust regex adding the -, for example:
String regex = "-WARN-|-PREVENT-";

Regular expression based detection [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I need to identify a pattern in given text (string), and I'm looking for a regex for the same. Using a Regex is preferable due to the framework I'm working in.
For instance, consider the text --
Problem:
<<< empty line(s) >>>>
Reason:
here goes some multi-line reasoning...
...
...
As you can see there is "no text (empty line(s)) after Problem: and before Reason: ".
I need to be able to identify this pattern from the text given to me, using a regex.
Any help is much appreciated.
Thanks!
The simplest regex would be
Pattern regex = Pattern.compile("Problem:\\s+Reason:");
which finds the text Problem:, followed by one or more whitespace characters, followed by the text Reason:.
If you want to make sure that there are at least two linebreaks between the two texts, you could also do
Pattern regex = Pattern.compile("Problem:[ \t]*\r?\n[ \t]*\r?\nReason:");
but that's probably not necessary.

Categories