Remove the text before the second comma ('', ") String replace pattern

how can we remove the text before the line starting with the second comma (line 5 in the example), how can I do this with regex? Example:

,
abc,xyz,ggg,nrmr
cde,jjj,kkkk,iiii,tem,posting
234,mm/dd/yy
,
454654,output2,sample
45646,output1,non-sample
16546,225.02
ABC,2.98
Expected

:

454654,output2,sample
45646,output1,non-sample
16546,225.02
ABC,2.98
+4
source share
1 answer

It seems you can use

val s = """,
abc,xyz,ggg,nrmr
cde,jjj,kkkk,iiii,tem,posting
234,mm/dd/yy
,
454654,output2,sample
45646,output1,non-sample
16546,225.02
ABC,2.98"""
val res = s.replaceFirst("(?sm)\\A(.*?^,$){2}", "").trim()
println(res)
// => 
//   454654,output2,sample
//   45646,output1,non-sample
//   16546,225.02
//   ABC,2.98

See Scala demo .

Template Details :

  • (?sm)- sallows to .fit any char in a row consisting of a new row lines and mmakes ^and $coincidence of the beginning / end line respectively
  • \\A - beginning of line
  • (.*?^,$){2} - 2 entries:
    • .*? - 0+
    • ^,$ - , ,.
+1

Source: https://habr.com/ru/post/1681833/


All Articles