Comments (6)
I added a private method to the Pattern class to solve the issue as follow:
static private boolean isInsideQuote(String s, int pos) {
boolean openQuoteFound = false;
boolean closeQuoteFound = false;
// find last non-escaped open-bracket
String s2 = s.substring(0, pos);
int posOpen = pos;
while ((posOpen = s2.lastIndexOf("\\Q", posOpen - 1)) != -1) {
if (!isEscapedChar(s2, posOpen)) {
openQuoteFound = true;
break;
}
}
if (openQuoteFound) {
// search remainder of string (after open-bracket) for a close-bracket
String s3 = s.substring(posOpen, pos);
int posClose = -1;
while ((posClose = s3.indexOf("\\E", posClose + 1)) != -1) {
if (!isEscapedChar(s3, posClose)) {
closeQuoteFound = true;
break;
}
}
}
return openQuoteFound && !closeQuoteFound;
}
and make small changes to isInsideCharClass method as below:
static private boolean isInsideCharClass(String s, int pos) {
boolean openBracketFound = false;
boolean closeBracketFound = false;
// find last non-escaped open-bracket
String s2 = s.substring(0, pos);
int posOpen = pos;
while ((posOpen = s2.lastIndexOf('[', posOpen - 1)) != -1) {
if (!isEscapedChar(s2, posOpen) && !isInsideQuote(s2, posOpen)) {
openBracketFound = true;
break;
}
}
if (openBracketFound) {
// search remainder of string (after open-bracket) for a close-bracket
String s3 = s.substring(posOpen, pos);
int posClose = -1;
while ((posClose = s3.indexOf(']', posClose + 1)) != -1) {
if (!isEscapedChar(s3, posClose) && !isInsideQuote(s3, posClose)) {
closeBracketFound = true;
break;
}
}
}
return openBracketFound && !closeBracketFound;
}
Correct me if I am wrong. Pardon my poor English for bad explanation. Thank you very much for this project. :)
from named-regexp.
This is not a valid pattern. You should get a Java compile error.
Pattern p = Pattern.compile("(?\Q[\E)(?\d+)(?\-)(?\d+)(?\])");
from named-regexp.
Sorry! I think the editor hide extra back-slashes. It should be like this:
Pattern p = Pattern.compile("(?\Q[\E)(?\d+)(?\-)(?\d+)(?\])");
I think the problem here is when you check a char using isInsideCharClass, we should also consider "\Q[\E" case. Otherwise, countOpenParens method will not work properly. That is why I added a new method called isInsideQuote, which is very similar to your isInsideCharClass.
from named-regexp.
That pattern still isn't valid syntax (and is also missing the group names that your debug-code is referencing). Can you provide a working code sample with a JUnit test?
Thanks.
from named-regexp.
Sorry again for my mistake! In fact, I am not so used to this editor. I have just fixed the original post to this issue. Please refer to that.
Thanks for your patient. :)
from named-regexp.
Fixed for 0.2.3. Thanks
from named-regexp.
Related Issues (13)
- Named group index is incorrect after parens in a character class HOT 2
- Getting runtime crash in release build HOT 3
- Up to date release HOT 1
- OOME when group matches empty string HOT 1
- Escaped characters get "un-escaped" when using Matcher.appendReplacement HOT 4
- Infinite loop when using Matcher.namedGroups HOT 2
- Broken backward compatibility of `namedGroups()` method HOT 1
- com.google.code.regexp.Pattern class isn't Serializable (java.util.regex.Pattern is) HOT 5
- Dependency Dashboard
- Exposing java.util.regex.Pattern.CASE_INSENSITIVE as Pattern.CASE_INSENSITIVE HOT 4
- Matcher#namedGroups() returns only first matching subsequence HOT 6
- Runs out of memory, when no group is present in the regex HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from named-regexp.