0

For example,we have a String name test as given below

def test = "HAVING first_name like "%Hello,Ram%" AND last_name like "%XYZ%",order by id asc,name desc"

So,after splitting i would like to have a result like

test1 = HAVING first_name like "%Hello,Ram%" AND last_name like "%XYZ%"

test2 = order by id asc,name desc

How to split such strings in grooyy? I tried splitting it by checking contains(",") and using the split function, but it would also split "%Hello,Ram%" which is not the result required

def a = 'HAVING first_name like "%Hello,Ram%" AND last_name like "%XYZ%",order by id asc,name desc'

def v = a.contains(",")

if(v){
 a.split(",")
 println a
}​​​
injecteer
  • 20,038
  • 4
  • 45
  • 89
  • 1
    Please add the code you have tried. – cfrick Apr 09 '20 at 14:03
  • @cfrick I have updated the code block in my above question. If i didnot had "%Hello,Ram%" on my string i could have used the output of the above program and manipulated the output.But i cant do it as such in this case. – Sirshak Regmi Apr 09 '20 at 16:07
  • 1
    Split does not mutate `a` but returns an array with the result. if you dont assign the result, this is a no-op. – cfrick Apr 09 '20 at 16:40
  • @cfrick so is there any way of returning an array like `[HAVING first_name like "%Hello,Ram%" AND last_name like "%XYZ%",order by id asc,name desc]`.If we are able to split it as such in the array we can do the other part.Like maybe if can get my desired result from array[1] as order by id asc,name desc .Please let me know if you have any solution. – Sirshak Regmi Apr 09 '20 at 17:43
  • 1
    The result of `split` is this – cfrick Apr 09 '20 at 18:33
  • Are you asking how to split on commas that are not inside % delimiters? Though if you are I'm not sure why you're not including a split on asc,name in your desired output. – Daniel Apr 09 '20 at 19:02
  • @Daniel yeah i wanted to split on commas that are not inside % delimiters.Yeah i could have used on asc,name but this values are dynamic values.It can be anything but the structure of the string would be as i mentioned in the question.So,is there any way i can split from %" these characters. – Sirshak Regmi Apr 10 '20 at 02:54

1 Answers1

0

Based on your comments, it sounds like what you really want to do is find commas that are not inside %xxx% delimiters and split on them. This is effectively a duplicate of Regex find comma not inside quotes which has more info on how the regex works.

def r = "(?!\\B%[^%]*),(?![^%]*%\\B)"
def test = """HAVING first_name like "%Hello,Ram%" AND last_name like "%XYZ%",order by id asc,name desc"""

def result = test.split(r)

In this, your result will be an array of strings (I'm not quoting them here, for clarity):

[
  HAVING first_name like "%Hello,Ram%" AND last_name like "%XYZ%"
  order by id asc
  name desc
]

The point I was trying to make in my comment is that the comma between asc and name is not inside percent signs, and therefor will be split on as well.

Daniel
  • 3,312
  • 1
  • 14
  • 31