Home > other >  Google Sheets REGEXEXTRACT splits into two cells
Google Sheets REGEXEXTRACT splits into two cells

Time:12-09

I have cell B10 with text that contains slashes and I want to extract the text around those slashes.

Example: Text in B10 is PV AA/BB 1250/2500/13 and I want to extract 1250/2500 in cell O10.

I tried to do =REGEXEXTRACT(B10, "([1-9][0-9][0-9][0-9])\/([1-9][0-9][0-9][0-9])") but for some reason, this splits the result into two cells (O10 and O11), without the /. Similar happens when I try to extract the AA/BB with ([a-zA-Z] )\/([a-zA-Z] ) - it returns AA and BB in two separate cells.

What is happening here? How do I extract the string into one cell only, with the slash?

CodePudding user response:

you have extra brackets "()" in your regular expression it should be,

=REGEXEXTRACT(B10, "([1-9][0-9][0-9][0-9]\/[1-9][0-9][0-9][0-9])")

the brackets ")(" before and after / is removed to get the desired result

CodePudding user response:

try:

=REGEXEXTRACT(B10; "\d{4}.\d{4}")

enter image description here


or:

=REGEXEXTRACT(B10; " (\d .\d )")

CodePudding user response:

You can 2 capture groups, but for a match you don't need any groups, and the / also does not have to be escaped.

=REGEXEXTRACT(B10, "[1-9]\d{3}/[1-9]\d{3}")

enter image description here


Or use REGEXREPLACE with the 2 capture groups and the / in the replacement and matching all chars before and after it so that they won't be present after the replacement.

=REGEXREPLACE(B10, ".*([1-9]\d{3})/([1-9]\d{3}).*", "$1/$2")

enter image description here

  • Related