Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use group 1 for Issue matching #151

Open
AndreasScharfCPB opened this issue Jun 7, 2023 · 2 comments
Open

Use group 1 for Issue matching #151

AndreasScharfCPB opened this issue Jun 7, 2023 · 2 comments

Comments

@AndreasScharfCPB
Copy link

Hi,
I am currently trying to parse our issues from the commit messages and that works. But because our format is [ABC-1234] we get [ABC-1234] as a result because the issue matcher returns group() (=group(0)).
In regex otherwise we could define non-capturing groups ( "(?:pattern)" ) which would result in this matching pattern NOT returned by the group.
The only difference for the default regex would be that it always have to be in one group aka surrounded by brackets.
So \b[a-zA-Z]([a-zA-Z]+)-([0-9]+)\b
would be (\b[a-zA-Z]([a-zA-Z]+)-([0-9]+)\b) and group(1) for ABC-1234 would return ABC-1234
and in our case we could use (?:\[)(([a-zA-Z](?:[a-zA-Z]+)-(?:[0-9]+)))(?:\]) and group(1) for [ABC-1234] would return ABC-1234

This way there would be more flexibility in the regex. The other, more complicated approach would be to also add a parameter for which group should be returned or return all groups or something but in my understanding group(1) should do the trick for the default and most other cases as you could just filter out everything else.

Code change would be here:

final String matchedIssue = issueMatcher.group();

@tomasbjerre
Copy link
Owner

Sorry, I dont understand what you want with this issue.

@AndreasScharfCPB
Copy link
Author

If you wrap the issue ID in special characters, like we do (e.g. [ABC-1234]). because we have several issue tracking systems with different formats, than \b(regex)\b does not work as the issue ID is not a word but \b[(regex)]\b does would work. But then the brackets are part of the issue id which is wrong and does not work down the road with loading Jira issue titles etc.
Just (regex) would also work but than of course it could match some other part of the commit message as well unintentionally.
issueMatcher.group() just returns the whole match.
issueMatcher.group(1) returns the first group of the matching String. So as I stated above you could define a regex that matches the String [(regex)] but groups only the real issueID and uses non-capturing groups to cut of the parts of the block that are not part of the issue ID.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants