TemplateRegexMatcher.getStartRegex sometimes returns a regex that matches with an index before the license start
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 71
- Forks
- 44
- Avg merge
- 12h 54m
- Merged PRs (30d)
- 7
Description
Version 1.1.11
Example 1 greedy regex after optional:
String licenseText = "ab cd text";
String licenseTemplate = "<<beginOptional>>cd<<endOptional>> <<var;name=\"copyright\";original=\"Copyright (c) <year> <copyright holders> \";match=\".{0,5000}\">> text";
TemplateRegexMatcher templateRegexMatcher = new TemplateRegexMatcher(licenseTemplate);
String startRegex = templateRegexMatcher.getStartRegex(25);
System.out.println("start regex: " + startRegex);
Matcher matcher = Pattern.compile(startRegex).matcher(licenseText);
if (matcher.find()) {
System.out.println("start index found: " + matcher.start());
}
Returns
start regex: (?im)(\Qcd\E\s*)?(.{0,5000})\Qtext\E\s*
start index found: 0
but the start index should be 3.
Example 2 greedy regex at start:
String licenseText = "abtext";
String licenseTemplate = "<<var;name=\"copyright\";original=\"Copyright (c) <year> <copyright holders> \";match=\".{0,5000}\">> text";
TemplateRegexMatcher templateRegexMatcher = new TemplateRegexMatcher(licenseTemplate);
String startRegex = templateRegexMatcher.getStartRegex(25);
System.out.println("start regex: " + startRegex);
Matcher matcher = Pattern.compile(startRegex).matcher(licenseText);
if (matcher.find()) {
System.out.println("start index found: " + matcher.start());
}
Returns
start regex: (?im)(.?{0,5000})\Qtext\E\s*
start index found: 1
but the start index should be 2.
.?{0,5000} doesn't seem to work as expected. It is an unusual regex that some online regex websites say is invalid: https://regex101.com/r/l3810b/1, regexr.com/81kfo.
https://www.freeformatter.com/java-regex-tester.html says the regular expression is valid.
I think maybe to fix this you could just offer a method for a regex to find the beginning of the non-optional part. Otherwise a changing the regular expressions in these two cases to something like the following could work
(?im)((\Qcd\E\s*)(.{0,5000})\Qtext\E\s*)|(\Qtext\E\s*)
(?im)\Qtext\E\s*
In the first case if there were multiple optional parts it would get even more complicated to do it correctly.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with TemplateRegexMatcher.getStartRegex and reproduce both Java examples from the issue. Trace how optional sections and greedy variable matches are converted into the start regex; done means the examples report start indices 3 and 2, including correct behavior when optional parts are present.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100