Closed matthew-elisha closed 5 months ago
Hello Matthew, There is no group called number on the lowest level. Number is only a group nested in another group. You would have to inspect the capture tree to see what the group number actually captured!
All the best, Florian
All
On Sat, Jan 9, 2021 at 1:01 PM Matthew Elisha notifications@github.com wrote:
I tested the following code Matcher matcher = Pattern.compile("(?x)" + "(?(DEFINE)" + "(?
(?'summand')(?:\+(?'summand'))+ )" + "(? (?'product') | (?'number') )" + "(? (?'factor')(?:\(?'factor'))+ )" + "(? (?'number') )" + "(? 8"); matcher.matches();\d++)" + ")" + "(?'sum')").matcher("5+6 But the following line was simply returning NULL: System.out.Println(matcher.group("number"));
Please what am I getting wrong?
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/florianingerl/com.florianingerl.util.regex/issues/10, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE6YEWSHULHKJCAHBKTH3GLSZBAP3ANCNFSM4V3REJNQ .
Thanks alot.
Hi,
I still have an issue pls. The captured group value for all groups above index 1 return null. Is that normal? How can I access the captured groups by name using the following pattern:
`Matcher matcher = Pattern.compile("(?x)" + "(?(DEFINE)" + "(?
EDIT:
My point is that I want to be able to access the value of a captured subsequence of the recursive pattern above by using something like this:
matcher.group('name');
Hello,
Any response to the above please?
You have to inspect the capture tree to see what these groups captured!
On Mon, Jan 11, 2021 at 9:24 AM Matthew Elisha notifications@github.com wrote:
Hello,
Any response to the above please?
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/florianingerl/com.florianingerl.util.regex/issues/10#issuecomment-757703839, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE6YEWRH4L42CN26IFPH5MLSZKYTXANCNFSM4V3REJNQ .
Hello,
I agree with matthew-elisha assuming that he invoked Matcher.find
or that like.
Whereas groups work for Matchers they don't for MatchResults, which is quite clear
looking at the implementation of Matcher.toMatchResult().
I was happy to see that MatchResult.group(String) promises to get named group matches, but the problem is also with numbered groups
Example:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.regex.MatchResult;
// import com.florianingerl.util.regex.Pattern;
// import com.florianingerl.util.regex.Matcher;
// import com.florianingerl.util.regex.MatchResult;
public class test {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("a(\\w+)b");
Matcher matcher = pattern.matcher("ahellob");
System.out.println("match: " + matcher.find());
System.out.println("group1: "+matcher.toMatchResult().group(1));
}
}
prints hello
as expected.
For the ingerl classes, the result is null
, because the MatchResult is an unmatched matcher, no find was applied yet.
It shall be hello
.
The same is true for named groups.
A fix I would accept just that Matcher.toMatchResult()
returns this
.
It is not really safe because one could access the internals by casting,
but I consider this criminal.
You could also copy the relevant pieces of information.
The latest commit should fix the problem
On Thu, Dec 7, 2023 at 8:30 PM Ernst Reissner @.***> wrote:
Hello, I agree with matthew-elisha https://github.com/matthew-elisha. Whereas groups work for Matchers they dont for MatchResults, which is quite clear looking at Matcher.toMatchResult().
I was happy to see that MatchResult.group(String) promises to get named group matches, but the problem is also with numbered groups
Example:
import java.util.regex.Pattern; import java.util.regex.Matcher; import java.util.regex.MatchResult;
// import com.florianingerl.util.regex.Pattern; // import com.florianingerl.util.regex.Matcher; // import com.florianingerl.util.regex.MatchResult;
public class test { public static void main(String[] args) { Pattern pattern = Pattern.compile("a(\w+)b"); Matcher matcher = pattern.matcher("ahellob"); System.out.println("match: " + matcher.find()); System.out.println("group1: "+matcher.toMatchResult().group(1)); } }
prints hello as expected. For the ingerl classes, the result is null, because the MatchResult is an unmatched matcher. It shall be hello.
The same is true for named groups.
A fix I would accept is just that Matcher.toMatchResult() returns this. It is not really safe because one could access the internals by casting, but I consider this criminal. You could also copy the relevant pieces of information.
— Reply to this email directly, view it on GitHub https://github.com/florianingerl/com.florianingerl.util.regex/issues/10#issuecomment-1845978063, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE6YEWVLUT53WGUHAYCATOTYIIKORAVCNFSM4V3REJN2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBUGU4TOOBQGYZQ . You are receiving this because you commented.Message ID: @.*** com>
You are really fast! Ah, not as minimal as I suggested. Better..
Hm.. did you publish? which version? 1.1.10?
Just to be sure: does it work for named groups also? According testcase maybe a good idea.
Hello Reissner, I just managed to deploy a new version where the bug is fixed. It is version 1.1.11 and can now be downloaded from Maven Central. This was a lot of work All the best, Florian
New version 1.1.11 can be downloaded from Maven central where the bug is fixed.
I tested the following code `Matcher matcher = Pattern.compile("(?x)" + "(?(DEFINE)" + "(? (?'summand')(?:\+(?'summand'))+ )"
But the following line was simply returning NULL:
System.out.Println(matcher.group("number"));
Please what am I getting wrong?