-1

I have a long list of words, characters, and new lines, how do tell the regex to only search the text between TWO EXACT WORDS on the list?

This is what I came up with but it does NOT stop searching"

(?!color).*id":(.*?),\s.*name": "(.*?)",\s.*\s.*miles

example: I want the REGEX ton return the following and stop searching after it reaches the word color again.

Matches:

1159 SAM
1597 ROBERT
1423 CARLOS

Code:

I have a long list of words, characters, and new lines, how do tell the regex to only search the text between TWO EXACT WORDS on the list?

Example:

I want the REGEX to return the following and stop searching after it reaches the word color again.

Matches:

1159 SAM
1597 ROBERT
1423 CARLOS

Code:

{
"groups": [{
    "id": 113,
    "name": "CAPISCO",
    "color": "#07380f",
    "params": [{
        "id": 1559,
        "name": "SAM.",
            "km": "1500",
            "miles": "0"
        }, {
            "km": "900",
            "miles": "0"
        }, {
            "km": "320",
            "miles": "0"
}, {
        "id": 1597,
        "name": "ROBERT.",
            "km": "1500",
            "miles": "0"
        }, {
            "km": "900",
            "miles": "0"
        }, {
            "km": "320",
            "miles": "0"
}, {
        "id": 1423,
        "name": "CARLOS.",
            "km": "1500",
            "miles": "0"
        }, {
            "km": "900",
            "miles": "0"
        }, {
            "km": "320",
            "miles": "0"
"id": 113,
    "name": "CORBAEU",
    "color": "#09854",
 "id": 113,
    "name": "SALUD",
    "color": "#07380f",
        "id": 1012,
}, {


            "km": "1500",
            "miles": "0"
        }, {
            "km": "900",
            "miles": "0"
        }, {
            "km": "320",
            "miles": "0"
}, {
        "id": 98,
        "name": "ANNIE.",
            "km": "1500",
            "miles": "0"
        }, {
            "km": "900",
            "miles": "0"
        }, {
            "km": "320",
            "miles": "0"
}, {
        "id": 1367,
        "name": "PETE.",
            "km": "1500",
            "miles": "0"
        }, {
            "km": "900",
            "miles": "0"
        }, {
            "km": "320",
            "miles": "0"

        "id": 866,
        "name": "Jay.",
            "km": "1500",
            "miles": "0"
        }, {
            "km": "900",
            "miles": "0"
        }, {
            "km": "320",
            "miles": "0"
}, {
        "id": 15933,
        "name": "JEAN.",
            "km": "1500",
            "miles": "0"
        }, {
            "km": "900",
            "miles": "0"
        }, {
            "km": "320",
            "miles": "0"
  • The formatting and length of this question are going to make most users give it a -1 or try to close it. Please edit and clean up the question. – Eric F May 31 '16 at 19:40
  • SORRY! I don't know how to format my code!!! – krimo Vasquez May 31 '16 at 19:57
  • 1
    There are buttons over the editor window, use them. – gronostaj May 31 '16 at 19:58
  • I have cleaned up your post some. Hopefully that helps – Eric F May 31 '16 at 20:03
1

Regex is a pattern matching language. With it, you only define a pattern. There are no means of keeping a counter to remember the number of matches - that is solely up to the implementation using the regex engine - for instance you can write a bit of code in any regex-supporting programming language to keep count of your matches and stop after n matches have been found. You can only construct a regex that will match a given group exactly a predefined number of times by means pattern repetition. In other words - repeat your pattern three times, to get three matches. For example:

  • Base pattern for match: (expr)
  • Pattern for one match between boundary words: boundaryword(?:.*?\s)+?(expr)(?:.*?\s)+?boundaryword
  • Pattern for two matches between boundary words: boundaryword(?:.*?\s)+?(expr)(?:.*?\s)+?(expr)(?:.*?\s)+?boundaryword
  • Pattern for threematches between boundary words: boundaryword(?:.*?\s)+?(expr)(?:.*?\s)+?(expr)(?:.*?\s)+?(expr)(?:.*?\s)+?boundaryword

et cetera... In your case 'boundaryword' is 'color'.

There is no way with regex to tell it to find an unknown number of matches and then stop at a given word. Typical regex methods either find just the first match, or all pattern matches in a document. Your problem requires a custom implementation. In a programming language you could write a loop that first matches boundaryword's position, from there matches your pattern, and keeps matching that pattern to find consecutive elements until it approaches the next boundaryword, where it stops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.