Regex not working for me -


i trying make regular expression find out third delimiter "/" , if match found grab entire sentence before , delete rest.

black / white / green / blue / delta / orange / yellow / pink...(n) 

outcome should

black / white / green 

i tried (.*(\/{2,3})) did not work.

your (.*(\/{2,3})) matches chars (perhaps, other newline) , 2 or 3 / symbols, capturing whole match group 1, , forward slashes group 2. may find match anywhere inside larger string, matches start of string last 2 or 2 / symbols on line.

you can use

^((?:[^\/]*\/){3}).* 

and replace $1 backreference.

see regex demo

depending on input , regex engine, might need dotall modifier . match newlines, too.

note can use simpler ^((?:[^\/]*\/){3}) matching substring.

also, if have access raw programming code, split string /, take first 3 elements, , join them /.


Comments