https://regex101.com/r/sB9wW6/1(?:(?<=\s)|^)@(\S+)
<-正向后看的问题
在prod
:(?:\s|^)@(\S+)
上像这样工作,但是我需要一个正确的开始索引(没有空间)。
在JS中:
var regex = new RegExp(/(?:(?<=\s)|^)@(\S+)/g);
解析正则表达式时出错:无效的正则表达式:
/(?:(?<= \ s)| ^)@(\ S +)/
我究竟做错了什么?
更新
好吧,在JS中没有后顾之忧:(
但是无论如何,我需要一个正则表达式来获取比赛的正确开始和结束索引。没有领先的空间。
最佳答案
确保始终在regex101.com上选择正确的regex引擎。查看由于使用JS-only compatible regex with [^]
construct in Python而发生的问题。
JS正则表达式-在回答此问题时-不支持lookbehinds。现在,它在its introduction in ECMAScript 2018之后越来越多地被采用。您实际上并不需要它,因为您可以使用捕获组:
var re = /(?:\s|^)@(\S+)/g;
var str = 's @vln1\n@vln2\n';
var res = [];
while ((m = re.exec(str)) !== null) {
res.push(m[1]);
}
console.log(res);
(?:\s|^)@(\S+)
将空白或字符串的开头与(?:\s|^)
匹配,然后与@
匹配,然后与(\S+)
匹配并将一个或多个非空白char捕获到组1中。要获取开始/结束索引,请使用
var re = /(\s|^)@\S+/g;
var str = 's @vln1\n@vln2\n';
var pos = [];
while ((m = re.exec(str)) !== null) {
pos.push([m.index+m[1].length, m.index+m[0].length]);
}
console.log(pos);
奖金
我的正则表达式可在regex101.com上使用,但不能在...
All languages - "Literal string" vs. "String literal" alert
-确保在正则表达式测试器中针对代码中使用的相同文本(文字字符串)进行测试。常见的情况是将string literal值直接复制/粘贴到测试字符串字段中,并使用所有字符串转义序列,例如\n
(换行符),\r
(回车),\t
(制表符)。请注意,必须将其替换为文字对应的内容。因此,如果您使用的是Python text = "Text\n\n abc"
,则必须在正则表达式测试器文本字段中使用Text
,两个换行符和abc
。 Text.*?abc
will never match it,尽管您might think it "works"。是的,.
并不总是与换行符匹配,请参见How do I match any character across multiple lines in a regular expression?All languages - Backslash alert
-确保在字符串文字中正确使用反斜杠,在大多数语言中,在常规字符串文字中,请使用双反斜杠,即regex101.com上使用的\d
必须写为\\d
。在原始字符串文字中,请使用一个反斜杠,与regex101相同。转义单词边界非常重要,因为在许多语言(C#,Python,Java,JavaScript,Ruby等)中,"\b"
用于定义BACKSPACE字符,即是有效的字符串转义序列。 PHP不支持\b
字符串转义序列,因此"/\b/"
= '/\b/'
在那里。All languages - Default flags - Global and Multiline
-请注意,默认情况下,regex101.com上启用了m
和g
标志。因此,如果使用^
和$
,它们将分别在行的开头和结尾匹配。如果在代码中需要相同的行为,请检查如何实现多行模式,并使用特定的标志,或者(如果支持的话)使用嵌入式(?m)
嵌入式(嵌入式)修饰符。 g
标志启用多次匹配,通常使用特定的功能/方法来实现。检查您的语言参考以找到合适的语言参考。line-breaks-regex101.com的行结尾仅是LF,您不能测试带有CRLF结尾的字符串,请参见regex101.com VS myserver - different results。每个正则表达式库的解决方案可能不同:使用
\R
(PCRE,Java,Ruby)或某种\v
(Boost,PCRE),\r?\n
,(?:\r\n?|\n)
/ (?>\r\n?|\n)
(适用于.NET) )或其他库中的[\r\n]+
(请参见C#,PHP的答案)。与针对多行字符串(而不是独立的字符串/行的列表)测试正则表达式有关的另一个问题是,您的模式可能会占用行号\n
的末尾字符,而字符类为负数,请参见an issue like that 。 \D
与行尾匹配,为了避免出现这种情况,可以使用[^\d\n]
或其他替代方法。php-您正在处理Unicode字符串,或者也想让速记字符类匹配Unicode字符(例如
\w+
匹配Стрибижев
或Stribiżew
或\s+
匹配硬空格),那么您需要要使用u
modifier,请参见preg_match() returns 0 although regex testers work-要匹配所有匹配项,请使用preg_match_all
,而不是preg_match
与/...pattern.../g
,请参见PHP preg_match to find multiple occurrences和"Unknown modifier 'g' in..." when using preg_match in PHP?php laravel-请注意,您需要在模式周围使用正则表达式定界符,请参阅https://stackoverflow.com/questions/22430529
python-您使用的
re.match
仅在字符串开头搜索匹配项,请使用re.search
:Regex works fine on Pythex, but not in Python-如果正则表达式包含捕获组,则re.findall
返回捕获列表/捕获元组。使用非捕获组或re.finditer
,或删除冗余捕获组,请参见re.findall behaves weirdc#,.net-.NET正则表达式不支持所有格修饰符,例如
++
,*+
,??
,{1,10}?
,请参见.NET regex matching digits between optional text with possessive quantifer is not working-与多行字符串匹配并使用RegexOptions.Multiline
选项(或内联(?m)
修饰符)在模式中带有$
锚以匹配整行,并且在代码中不匹配,您需要在\r?
之前添加$
,请参见.Net regex matching $ with the end of the string and not of line, even with multiline enabled-类似的情况上图:通过双换行符序列将字符串拆分为段落-C# / Regex Pattern works in online testing, but not at runtime-您应删除正则表达式定界符,即@"/\d+/"
必须实际上看起来像@"\d+"
,请参见Simple and tested online regex containing regex delimiters does not work in C# code-如果您不必要地使用了Regex.Escape
来请删除Regex.Escape(@"\d+\.\d+")
,以转义正则表达式中的所有字符(例如Regex.Escape
),请参见Regular Expression working in regex tester, but not in c#dart flutter-使用原始字符串文字,
RegExp(r"\d")
或双反斜杠(RegExp("\\d")
)-https://stackoverflow.com/questions/59085824javascript-
RegExp("\\d")
中的双转义反斜杠:Why do regex constructors need to be double escaped?-大多数浏览器不支持的(负)回溯:Regex works on browser but not in Node.js-字符串是不可变的,将
.replace
结果分配给var-The .replace() method does change the string in place-使用str.match(/pat/g)
-Regex101 and Js regex search showing different results或RegExp#exec
检索所有匹配项, RegEx to extract all matches from string using RegExp.exec-替换字符串中的所有模式匹配项:Why does javascript replace only first instance when using replace?javascript angular-如果您使用字符串文字定义正则表达式,或者仅使用正则表达式文字符号,则将反斜杠加倍,请参阅https://stackoverflow.com/questions/56097782
java-单词边界不起作用?确保使用双反斜杠
"\\b"
,请参见Regex \b word boundary not works-获取invalid escape sequence
异常?同样,双反斜杠-Java doesn't work with regex \s, says: invalid escape sequence-No match found
困扰您吗?运行Matcher.find()
/ Matcher.matches()
-Why does my regex work on RegexPlanet and regex101 but not in my code?-.matches()
需要完整的字符串匹配,请使用.find()
:Java Regex pattern that matches in any online tester but doesn't in Eclipse-使用matcher.group(x)
访问组:Regex not working in Java while working otherwise-在字符类中,都[
必须转义-Using square brackets inside character class in Java regex-不应连续运行]
和matcher.matches()
,仅使用matcher.find()
检查模式是否与整个字符串匹配,然后采取相应措施,或者使用if (matcher.matches()) {...}
检查是否有单个匹配项,或者if (matcher.find())
查找多个匹配项(或while (matcher.find())
)。见Why does my regex work on RegexPlanet and regex101 but not in my code?c++-
Matcher#results()
需要完整的字符串匹配,请使用regex_match
查找部分匹配-Regex not working as expected with C++ regex_match-使用regex_search
读取用户定义的字符串时,请注意std::string input; std::cin >> input;
仅会到达第一个空格,要正确读取整行,请使用cin
-C++ Regex to match '+' quantifier-std::getline(std::cin, input);
不起作用,您需要使用"\d"
或"\\d"
(原始字符串文字)-This regex doesn't work in c++go-双反斜杠或使用原始字符串文字:Regular expression doesn't work in Go-Go regex不支持环视,请在regex101.com上选择正确的选项(
R"(\d)"
),然后再进行测试! Regex expression negated set not working golanggroovy-返回所有匹配项:Regex that works on regex101 does not work in Groovy
r-字符串文字中的双转义反斜杠:"'\w' is an unrecognized escape" in grep-使用
Go
来PCRE引擎(perl=TRUE
/ (g)sub
):Why is this regex using lookbehinds invalid in R?oracle-所有量词的贪婪度由正则表达式中的第一个量词设置,请参见Regex101 vs Oracle Regex(然后,您需要使所有量词与第一个量词一样贪婪)
firebase-双转义反斜杠,确保
(g)regexpr
仅出现在模式的开头,而^
仅位于模式的末尾(如果有),并且请注意,您不能使用超过9个内联反向引用:Firebase Rules Regex Birthdaygoogle-data-studio-
$
中的/pattern/g
必须不包含REGEXP_REPLACE
正则表达式定界符和标志(例如/
)-请参见How to use Regex to replace square brackets from date field in Google Data Studio?sed-Why does my regular expression work in X but not in Y?
word-boundary pcre php-
g
和[[:<:]]
在正则表达式测试器中不起作用,尽管它们在PCRE中是有效的构造,请参见https://stackoverflow.com/questions/48670105
关于regex - 正则表达式适用于regex101.com,但不适用于prod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61124532/