简体   繁体   English

正则表达式适用于 JavaScript,但不适用于 Google 表格

[英]Regex works in JavaScript but not Google Sheets

I need a regex that can get the company name (in this case "google.com") from this format: company=google.com&extension我需要一个可以从以下格式获取公司名称(在本例中为“google.com”)的正则表达式: company=google.com&extension

According to https://regex101.com/ this should work: (?<=company).*(?=&ext)根据https://regex101.com/这应该可以工作: (?<=company).*(?=&ext)

However when I try =REGEXEXTRACT(B3, "(?<=company).*(?=&ext)") in Google Sheets I get an error:但是,当我在 Google 表格中尝试=REGEXEXTRACT(B3, "(?<=company).*(?=&ext)")时出现错误:

Function REGEXEXTRACT parameter 2 value "(?<=company).*(?=&ext)" is not a valid regular expression.函数 REGEXEXTRACT 参数 2 值“(?<=company).*(?=&ext)”不是有效的正则表达式。

Ive also tried =REGEXEXTRACT(B3, "/(?<=company).*(?=&ext)/")我也试过=REGEXEXTRACT(B3, "/(?<=company).*(?=&ext)/")

Function REGEXEXTRACT parameter 2 value "/(?<=company).*(?=&ext)/" is not a valid regular expression.函数 REGEXEXTRACT 参数 2 值“/(?<=company).*(?=&ext)/”不是有效的正则表达式。

The REGEXEXTRACT function does not support lookarounds so you have to use non-capturing groups for parts that should not be captured and captured groups. REGEXEXTRACT函数不支持环视,因此您必须对不应捕获的部分和捕获组使用非捕获组。

Just use (?:company)(.*)(?:&ext) instead of yours.只需使用(?:company)(.*)(?:&ext)而不是您的。 Also, you were using , after B3 .此外,您在B3之后使用, Do not know which version of Google Sheets you are using but it did not work for me and I had to change it to ;不知道您使用的是哪个版本的 Google 表格,但它对我不起作用,我不得不将其更改为;

=REGEXEXTRACT(B3; "(?:company)(.*)(?:&ext)")

NOTE: If you're using non-capturing groups then do not forget to use capturing group, otherwise it will return the whole matched values including non-capturing ones as well.注意:如果您使用的是非捕获组,请不要忘记使用捕获组,否则它将返回整个匹配值,包括非捕获值。

You could as well try the following:您也可以尝试以下方法:

=REGEXEXTRACT(B3,"=(\S*)?&")

This should extract only the specific string google.com or any word between the = sign and the & characters.这应该只提取特定字符串google.com=符号和&字符之间的任何单词。

You can match company and capture the part before the ampersand in a group:您可以匹配公司并捕获组中&符号之前的部分:

company=([^\s&]+)&ext

See a regex demo .查看正则表达式演示

在此处输入图像描述

One can also use the following也可以使用以下

=REGEXEXTRACT(B2,"=(.*)&")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM