Extract multiple values if present from cell in Google Spreadsheets

蹲街弑〆低调 提交于 2019-12-08 04:40:54

问题


Using Google re2 https://github.com/google/re2/blob/master/doc/syntax.txt

From a couple of lines like

  1. I love Rock
  2. I love Rock and scissors
  3. I hate paper
  4. I like Rock, paper and scissors
  5. I'd love myself

I want to extract "Rock", "paper"and "scissors" from each line. I want the regex to match all the above five lines and give me Rock, paper and scissors where it found something. I'm predominantly using this in Google sheets, but any Google re2 regex should help.

I've tried....

".*(([Rock]{0,4})).*"

".*(([Rock]{4})|([Rock]{0})).*"

=REGEXEXTRACT(A2,".*(Rock{0,2}).*(paper{0,2}).*(scissors{0,2}).*")

and multiple other combinations to get Rock from any line, if present... But, it always prefers zero rather than four... Even If it finds Rock , it returns empty strings. If i replace {0} with {1} i get "k" even though the full Rock is found.

Any ideas?


回答1:


I found some regex features are not supported in Google Sheets so far.

Please, try this workaround:

=ArrayFormula(IFERROR(REGEXREPLACE(A3,REGEXREPLACE(A3,"(Rock|paper|scissors)","(.*)"),{"$1","$2","$3"})))


In step 1 this formula makes regex for step2:




回答2:


[Rock] will search for letters R o c k. Instead, use (Rock)



来源:https://stackoverflow.com/questions/45456630/extract-multiple-values-if-present-from-cell-in-google-spreadsheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!