regex正则表达式在regex101上工作。com,但不是prod
https://regex101.com/r/sB9wW6/1
(?:(?<=\s)|^)@(\S+)
<;-积极回顾中的问题
在prod
:(?:\s|^)@(\S+)
上这样工作,但我需要一个正确的开始索引(没有空格)
以下是JS:
var regex = new RegExp(/(?:(?<=\s)|^)@(\S+)/g);
Error parsing regular expression: Invalid regular expression: /(?:(?<=\s)|^)@(\S+)/
我做错了什么
更新
好的,JS中没有后顾:(
但无论如何,我需要一个正则表达式来获得比赛的正确开始和结束索引。没有引导空间
# 1 楼答案
确保始终在regex101中选择正确的正则表达式引擎。com。查看由于使用JS-only compatible regex with ^{} construct in Python 而发生的问题
在回答这个问题时,JS regex-,不支持lookbehinds。现在,它在its introduction in ECMAScript 2018之后越来越多地被采用。您在这里并不真正需要它,因为您可以使用捕获组:
(?:\s|^)@(\S+)
将一个空格或字符串的开头与(?:\s|^)
匹配,然后与@
匹配,然后将(\S+)
匹配并捕获到组1中的一个或多个非空格字符要获取开始/结束索引,请使用
奖金
我的regex在regex101工作。com,但不是在
首先,您是否检查了左侧工具窗格中的代码生成器链接
{a3}
All languages - "Literal string" vs. "String literal" alert
-确保在正则表达式测试仪上测试代码中使用的相同文本,文本字符串。一种常见的情况是将string literal值直接复制/粘贴到测试字符串字段中,使用所有字符串转义序列,如\n
(换行字符),\r
(回车符),\t
(制表字符)。例如,见Regex_search c++。记住,它们必须被字面上的对应物所取代。因此,如果在Python中有text = "Text\n\n abc"
,那么必须在regex tester文本字段中使用Text
,两个换行符abc
Text.*?abc
{a6}尽管你{a7}。是的,.
并不总是匹配换行符,请参见How do I match any character across multiple lines in a regular expression?All languages - Backslash alert
-确保在字符串文本中正确使用反斜杠,在大多数语言中,在常规字符串文本中,使用双反斜杠,即在regex101中使用\d
。com必须写为\\d
。在原始字符串文本中,使用一个反斜杠,与regex101相同。转义单词边界非常重要,因为在许多语言(C#、Python、Java、JavaScript、Ruby等)中,"\b"
用于定义退格字符,即它是有效的字符串转义序列。PHP不支持\b
字符串转义序列,因此"/\b/"
='/\b/'
在那里All languages - Default flags - Global and Multiline
-请注意,默认情况下,m
和g
标志在regex101处启用。通用域名格式。因此,如果使用^
和$
,它们将在行的开始和结束处相应地匹配。如果代码中需要相同的行为,请检查多行模式是如何实现的,或者使用特定的标志,或者——如果支持的话——使用内联(?m)
嵌入(内联)修饰符。g
标志支持多次匹配,通常使用特定的函数/方法实现。检查您的语言参考以找到合适的语言参考line-breaks-行结尾位于regex101。com是LF-only,不能测试带有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} modifier ,请参见preg_match() returns 0 although regex testers work} ,而不是带有{}的{},请参见{a23}和{a24}}拒绝工作?您使用的是双引号字符串文字吗?使用单引号,参见Backreference does not work in PHP
您正在处理Unicode字符串,或者希望速记字符类也匹配Unicode字符(例如
\w+
匹配Стрибижев
或Stribiżew
,或\s+
匹配硬空格),然后需要使用^{要匹配所有出现的字符,请使用^{
-您的带有内联反向引用的正则表达式,如{
phplaravel
注意,您需要在模式周围使用正则表达式分隔符,请参见https://stackoverflow.com/questions/22430529
python,使用^{:Regex works fine on Pythex, but not in Python
使用只在字符串开头搜索匹配项的^{
如果正则表达式包含捕获组,
re.findall
返回捕获/捕获元组列表。使用非捕获组,或re.finditer
,或删除冗余捕获组,请参见re.findall behaves weird-如果在模式中使用
^
表示行的开始,而不是整个字符串的开始,或使用$
表示行的结束而不是字符串,请将re.M
或re.MULTILINE
标志传递给re
方法,请参阅Using ^ to match beginning of line in Python regex如果您试图跨多行匹配一些文本,并使用
re.DOTALL
或re.S
,或[\s\S]*
/[\s\S]*?
,但仍然没有任何效果,请检查是否逐行读取文件,例如使用for line in file:
。必须将整个文件内容作为输入传递给regex方法,请参见Getting Everything Between Two Characters Across New Lines在向正则表达式添加标志并尝试类似
pattern = r"/abc/gi"
的操作时遇到问题?见How to add modifers to regex in python?c#,.net,而不是^{,请参见RegEx Match multiple times in string必须看起来像^,参见Simple and tested online regex containing regex delimiters does not work in C# code
-。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
-你应该删除regex定界符,即^
——如果不必要地使用
Regex.Escape
来转义正则表达式中的所有字符(如Regex.Escape(@"\d+\.\d+")
),则需要删除Regex.Escape
,参见Regular Expression working in regex tester, but not in c#dartflutter
使用原始字符串文字、
RegExp(r"\d")
或双反斜杠(RegExp("\\d")
)-https://stackoverflow.com/questions/59085824javascript
在
RegExp("\\d")
:Why do regex constructors need to be double escaped?中使用双转义反斜杠 大多数浏览器不支持的(负面)lookbehind:Regex works on browser but not in Node.js字符串是不可变的,将
.replace
结果分配给var-The .replace() method does change the string in placeRetrieveall与
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?
javascriptangular
如果使用字符串文字定义正则表达式,或仅使用正则表达式文字符号,请参阅https://stackoverflow.com/questions/56097782
java} )。见Why does my regex work on RegexPlanet and regex101 but not in my code?
单词边界不工作?确保使用双反斜杠,
"\\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-在字符类中,
[
和]
必须转义^{,仅使用if (matcher.matches()) {...}
检查模式是否与整个字符串匹配,然后采取相应的行动,或使用if (matcher.find())
检查是否存在单个匹配,或使用while (matcher.find())
查找多个匹配(或^{scala
您的正则表达式试图匹配几行,但您逐行读取文件(例如,使用
for (line <- fSource.getLines)
)?将其读入单个变量(参见matching new line in Scala regex, when reading from file)kotlin
-你有
Regex("/^\\d+$/")
?删除外部斜线,它们是regex delimiter chars,不是模式的一部分。见Find one or more word in string using Regex in Kotlin-Yo你希望部分字符串匹配,但
.matchEntire
需要完全字符串匹配?使用.find
,参见Regex doesn't match in Kotlinmongodb
不要用单/双引号将
/.../
括起来,请参见mongodb regex doesn't workc++-C++ Regex to match '+' quantifier}不起作用,您需要使用
-
regex_match
需要完整的字符串匹配,使用regex_search
查找部分匹配-Regex not working as expected with C++ regex_match-
regex_search
仅查找第一个匹配。使用sregex_token_iterator
或sregex_iterator
获取所有匹配项:请参见What does std::match_results::size return?当您使用
std::string input; std::cin >> input;
读取用户定义的字符串时,请注意cin
只会获取第一个空格,要正确读取整行,请使用^{-^
"\\d"
或R"(\d)"
(原始字符串文本)-This regex doesn't work in c++-确保正则表达式是根据文本而不是字符串文本测试的,请参见Regex_search c++
go)。com测试前Regex expression negated set not working golang
双反斜杠或使用原始字符串文字:Regular expression doesn't work in Go
Go regex不支持lookarounds,请在regex101处选择正确的选项(^{
groovy
返回所有匹配项:Regex that works on regex101 does not work in Groovy
r
字符串文本中的双转义反斜杠:"'\w' is an unrecognized escape" in grep
使用
perl=TRUE
到PCRE引擎((g)sub
/(g)regexpr
):Why is this regex using lookbehinds invalid in R?oracle
所有量词的贪婪度由正则表达式中的第一个量词设置,请参见Regex101 vs Oracle Regex(然后,您需要使所有量词与第一个量词一样贪婪)
-
\b
不起作用吗?Oracle正则表达式根本不支持单词边界,请使用Regex matching works on regex tester but not in oracle中所示的变通方法firebase
双转义反斜杠,确保
^
仅出现在模式的开头,并且$
仅位于结尾(如果有),注意不能使用超过9个内联反斜杠:Firebase Rules Regex Birthdayfirebasegoogle-cloud-firestore
在Firestore安全规则中,正则表达式需要作为字符串传递,这也意味着它不应该被包装在
/
符号中,即使用allow create: if docId.matches("^\\d+$")
。。。。看见https://stackoverflow.com/questions/63243300google-data-studio
-
/pattern/g
中的REGEXP_REPLACE
必须不包含/
正则表达式定界符和标志(如g
)-请参见How to use Regex to replace square brackets from date field in Google Data Studio?google-sheets
如果你认为
REGEXEXTRACT
没有返回完整的匹配,截断结果,你应该检查你的正则表达式中是否有多余的捕获组并删除它们,或者在打开(
后通过添加?:
将捕获组转换为非捕获组,请参见Extract url domain root in Google Sheetsed
-Why does my regular expression work in X but not in Y?
word-boundary{a98}{a19}
-
[[:<:]]
和[[:>:]]
在正则表达式测试程序中不起作用,尽管它们在PCRE中是有效的结构,请参阅https://stackoverflow.com/questions/48670105snowflake-cloud-data-platformsnowflake-sql-如果您正在编写存储过程,而
\\d
不起作用,则需要再次将它们加倍,并使用\\\\d
,请参见REGEX conversion of VARCHAR value to DATE in Snowflake stored procedure using RLIKE not consistent