Why is REGEXEXTRACT returning a single value when it is expected to return an array of 1 row and multiple columns?

Deadly 提交于 2019-12-06 12:00:29

问题


The matrix management feature in Google Spreadsheets is convenient and combined with functions that handle regular expressions like REGEXMATCH and REGEXEXTRACT, among others, makes it especially useful.

I have a case in which I do not know what is due that does not give the expected result. Here's what I'm trying to do:

Spreadsheet Settings
Regional Settings: Mexico, use . (dot) as decimal separator.

Entry
A1: abcde

Formula
B1: =ArrayFormula(REGEXEXTRACT(A1,{".{1}",".{2}"}))

Expected result
B1: a
B2: ab

Obtained result
B1: a
B2:

Known workaround
=ArrayFormula(TRANSPOSE(REGEXEXTRACT(A1,{".{1}";".{2}"})))

This question also has being posted on the Spanish site -> https://es.stackoverflow.com/q/55704/65


回答1:


Quoting Jean-Pierre Verhulst on a similar case in REGEXEXTRACT Array Mysteriously Stopped Working Today (Google Docs Help Forum):

The team is well aware of the issue and a fix should be there soon.


Coincidentally, it was published Jan 4th, 2017, the same date AdamL modified his answer to ARRAYFORMULA() does not work with SPLIT(), explaining that:

REGEXEXTRACT no longer appears to support an array for the second argument.



We can conclude that this behaviour in ArrayFormula is due to a modification in Google Sheets, allowing SPLIT in array formulas, with the consequence of REGEXEXTRACT not accepting multiple columns as input in the regex.

This is probably because REGEXEXTRACT, with multiple capture groups in the regular expression, yields an horizontal array, one cell for each group. Having an array as argument, the behaviour may be undefined, but that is plain argumentative on my side.




回答2:


The reason your not seeing expected results in your particular formula, is the order in which you are using arrayformula and regexextract - you need to either modify your regex syntax to extract 2 groups, or you need to make an array to separate each regex extract function.

There are a few ways to do it, 1 way is to create a literal array and just specify the 2 extract patterns:

={REGEXEXTRACT(A1,"^."),REGEXEXTRACT(A1,"^.{2}")}

The other is to create 2 capture groups, only thing about the second one is by default is comes back in reversed order, but you can easily swap that by sort():

=REGEXEXTRACT(A1,"^((.).)")



来源:https://stackoverflow.com/questions/42826011/why-is-regexextract-returning-a-single-value-when-it-is-expected-to-return-an-ar

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