REGEX difficulty

If I use the REGEX tester <https://regex101.com/>

with regular expression (?=(29|79|97))

and test string 29791

I get 3 matches 29, 97,79, as I expect.

On the other hand, if I enter =REGEX("29791","(?=(29|79|97))",1)

I get a zero length string.

Can anyone help please?

David Lynch

Version: 7.1.3.2 (x64) / LibreOffice Community
Build ID: 47f78053abe362b9384784d31a6e56f8511eb1c1
CPU threads: 4; OS: Windows 10.0 Build 19041; UI render: Skia/Raster; VCL: win
Locale: en-GB (en_GB); UI: en-GB
Calc: threaded

What flavour of regex are we talking about?
Can you show us how you get three matches here.
I can only see that there will be one match, 29, 79 or 97.
Furthermore, if you intend to use this as a "lookahead", there is something
missing in the regex, the prefix that shall be matched if it is followed by
one of these numbers, and in this case, only 29 will make that prefix match.
If you provide the help description of the parameters to REGEX() I'm sure
we can find an explanation of its behaviour.

Hi David,

If I use the REGEX tester <https://regex101.com/>
with regular expression (?=(29|79|97))
and test string 29791
I get 3 matches 29, 97,79, as I expect.
On the other hand, if I enter =REGEX("29791","(?=(29|79|97))",1)

I think you have to escape the parentheses vis:

=REGEX("29791","\(?=\(29|79|97\)\)",1)

that yields 79

Regards,
Tony.

David Lynch writes:

On the other hand, if I enter =REGEX("29791","(?=(29|79|97))",1)

I get a zero length string.

Can anyone help please?

https://stackoverflow.com/questions/1570896/what-does-mean-in-a-regular-expression

(?=pattern) is a zero-width positive lookahead assertion. For example,
/\w+(?=\t)/ matches a word followed by a tab, without including the tab.

So it looks to me like you are trying to match nothing followed by a 29,
and you are successfully matching nothing.

=REGEX("29791","((78|87))",1) tells me "#N/A" , which I suppose is
what you get when a regex fails. Yours succeeds and returns an empty
string.

--hymie! http://nasalinux.net/~hymie hymie@nasalinux.net