-2

I am trying to take a string such as

Results where srn.firsttname like 'Chicken%' and coalesce(sri.DocumentType_id,0) not in (71, 72, 70, 69) and having srn.lastname like 'Alfredo%'

to return

Results where srn.lastname like 'Chicken%' and srn.firstname like 'Alfredo%'

using regex? I haven't found anything online to assist with this.

This is the code I have that is not regex:

function stringSlicer(userInput) {
    var frontStringLength = 0;
    var middlecut = 1;
    var slicer = 0;

    for (var i = 0; i < userInput.length; i++) {
        if (userInput.substring(i, 1) === "(") {
            frontStringLength = i - 13;
            break;
         }
    }

    for (var j = 0; j < userInput.length; j++) {
        if (userInput.substring(j, 1) !== ")") {
            middlecut += 1;
        }
        else if (userInput.substring(j, 1) === ")") {
            middlecut += 1;
            slicer += 1;
            if (slicer === 2) {
                break;
            }
        }
    }

    var result = userInput.substring(0, frontStringLength) +     userInput.substring(frontStringLength + middlecut);
    return result;
}

Thanks!!

chris85
  • 23,846
  • 7
  • 34
  • 51
cjw09
  • 1
  • 8
    Its completely unclear what logical rules dictate what text is to be removed – Alex K. Jan 13 '17 at 17:35
  • I am removing "and coalesce(sri.DocumentType_id,0) not in (71, 72, 70, 69) but the numbers can change" – cjw09 Jan 13 '17 at 17:36
  • @h2ooooooo I did give the input string as well as the expected string. this was the only way I could get the function to return the expected – cjw09 Jan 13 '17 at 17:39
  • @cjw09 I'm (we're?) willing to float you some extra slack because it seems like English is not your first language, but its not clear from your quoting what is the text your looking to replace and what is commentary *about* the text you're trying to replace. – Jared Smith Jan 13 '17 at 17:40
  • @JaredSmith I want to cut this --> and coalesce(sri.DocumentType_id,0) not in (71, 72, 70, 69) <-- the only problem is that the numbers will change and I do not understand regex. Sorry if I am coming off aggressive. – cjw09 Jan 13 '17 at 17:41
  • 1
    Is this SQL? Don't manipulate a language like SQL using regexp. –  Jan 13 '17 at 17:45
  • @cjw09 not aggressive at all, it was just very confusing. Thanks for clarifying, looks like you've got a couple of answers now. Also, totally agree with what torazaburo said, think long and hard about using regex on what is essentially source code... an SQL parser would likely serve you better. – Jared Smith Jan 13 '17 at 17:46
  • So `and coalesce(sri.DocumentType_id,0) not in (71, 72, 70, 69)` is consistent? aside from `69-79`? If so I think `and coalesce\(sri\.DocumentType_id,0\) not in \([\d, ]+\)` would do it, this doesn't validate the data in `not in` only that it is a number, comma, or space and in `()`s. – chris85 Jan 13 '17 at 18:13
  • @torazaburo: why not?? it's not like it's xml. – Scott Weaver Jan 13 '17 at 18:54

2 Answers2

0

divide up the string into parenthetical groups so that you can rearrange the results with your replacement string and matched groups. (string.match example here).

the pattern:

(.*?)(srn\.firsttname)( like '%?\w+%?' and )(.*?)(srn\.lastname)(.*)

the replacement string (invert groups 2 and 5 and throw away 4):

$1$5$3$2$6

demo

Community
  • 1
  • 1
Scott Weaver
  • 7,192
  • 2
  • 31
  • 43
-1

something like this?

mystring.replace(/(.+'Chicken%')(.+)(and.+)/g, "$1$3");

It'll just remove averything between Chicken% and the last and, assuming the strings are all similar.

  • even if the numbers are changing? Sorry I am continuing to read up on regex, I just don't get how it can work if there are changing items. – cjw09 Jan 13 '17 at 17:43
  • regex just look for for patterns in a string. The all regex syntax is just the way you tell the engine what to look for. It takes a while to gets the ins and outs of how regex works, but the time you spend on that will pay off. – Giacomo Cosimato Jan 13 '17 at 17:47