Regex

Options
Greg_703914
Greg_703914 Posts: 42
edited April 2021 in Questions

How can I get the regex to stop searching at the end of row/line?

see example here: https://regex101.com/r/bdXIs7/1/

I used this regex command "Consumer Max Multiplier Included.?[\S] .?[\S] *?(?=Yes|No)" because in some scenarios there may be a third yes or no that I need to capture in the Consumer Max Multiplier Included row that is not present in this particular case.

Answers

  • Aly_142377
    Aly_142377 Posts: 18 admin
    Options

    Hey @Greg_703914 !
    Does this achieve what you are looking for?

    Consumer Max Multiplier Included[^\r\n]+[a-zA-Z][a-zA-Z_0-9]+

  • Greg_703914
    Options

    @Aly_142377 I probably could have explained this better. I am actually after 4 independent regex commands that look ahead to each yes or no independently in the "Consumer Max Multiplier Included" row, where there can be up to 4 yes's or no's. In my OP I just showed you the regex that looks ahead for a third yes or no. The reason I showed this example is because in this example it shows that it is not stopping at the end of the line like I need it to. It continues to look for a yes and a no after this particular "Consumer Max Multiplier Included" row and I don't want it to look for any more yes's or no's after the line ends. Below are all four extractions as wrote them. One for each yes or no, moving left to right, for a maximum of 4 yes's or no's or combination thereof.

    Example of when there are four values:
    Value 1: https://regex101.com/r/My9glK/1
    Value 2: https://regex101.com/r/ogd3Yn/1
    Value 3: https://regex101.com/r/PbvjTJ/1
    Value 4: https://regex101.com/r/yGJAh0/1

    The extraction you wrote captures the whole line up to the last yes or no, but I actually need 4 separate commands to look ahead and locate each yes or no in sequence from left to right, 1 by 1 independently. The process that was built for us uses lookaheads to capture the lookahead value when I write them like the four examples in the links above. I just need each commands to stop looking ahead for a yes or no at the end of the line. Does that make sense?

  • Aly_142377
    Aly_142377 Posts: 18 admin
    Options

    Ah I understand. I would take a slightly different approach and use regex to get the single line you need and then use string functionality to parse out the Yes-es and Nos. The issue with the lookahead is that they are greedy, hence why they are not stopping at the end of line.

    const regEx = /Consumer Max Multiplier Included[^\r\n]+[a-zA-Z][a-zA-Z_0-9]+/g
    const str = `{{text}}`.match(regEx)[0]
    const subStringStart = str.indexOf('Included') + 'Included'.length
    let subString = str.substring(subStringStart, str.length)
    const arr = subString.trim().split(/\s+/g)
    const answer1 = arr[0]
    const answer2 = arr[1]
    const answer3 = arr[2]
    let answer4;
    if (arr.length === 4) {
    answer4 = arr[3]
    } else {
    answer4 = ''
    }
    

    That will get your Yes-es and No-s, regardless of how many there are in the line. Let me know if that solves the issue!

  • Greg_703914
    Options

    I don't think the process that was built for us has the ability to easily build in Javascript methods using the extracted text in the way you have suggested here. I would be happy to provide more detail on this and the process that was built for us. I actually have a meeting with Andrew Luedke about this because he provided a similar type of suggestion. I will offer to him that you could join.