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

Get pattern position with grok #26

Open
jgsqware opened this issue Feb 5, 2019 · 1 comment
Open

Get pattern position with grok #26

jgsqware opened this issue Feb 5, 2019 · 1 comment

Comments

@jgsqware
Copy link

jgsqware commented Feb 5, 2019

Hi, is it possible with grok to have the start position of each pattern, to be able to highlight only some portion of string for example?

@pchakour
Copy link

It's possible by patching the code.

You have to patch the index.js file. Find the parseSync method and replace it by the following :

function(str) {
        if (!t.regexp) {
            t.regexp = new OnigRegExp(t.resolved);
        }

        var result = t.regexp.searchSync(str);

        if(!result)
            return null;

        var r = {};

        result.forEach(function(item, index) {
            var field = t.fields[index];
            if(field && item.match) {
                r[field] = item;
            }
        });

        return r;
    };

It will return for each key an object lik this:

{ index: 78, start: 41, end: 44, length: 3, match: 'GET' }

where start is the start position, end the end position and match the matched value.

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