Coder Social home page Coder Social logo

Comments (6)

idiotX avatar idiotX commented on April 29, 2024

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.

tony19 avatar tony19 commented on April 29, 2024

This is not a valid pattern. You should get a Java compile error.

Pattern p = Pattern.compile("(?\Q[\E)(?\d+)(?\-)(?\d+)(?\])");

from named-regexp.

idiotX avatar idiotX commented on April 29, 2024

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.

tony19 avatar tony19 commented on April 29, 2024

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.

idiotX avatar idiotX commented on April 29, 2024

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.

tony19 avatar tony19 commented on April 29, 2024

Fixed for 0.2.3. Thanks

from named-regexp.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.