gpt4 book ai didi

regex - 正则表达式适用于regex101.com,但不适用于prod

转载 作者:行者123 更新时间:2023-12-01 17:30:49 25 4
gpt4 key购买 nike

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,两个换行符和 abcText.*?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#PythonJavaJavaScriptRuby等)中, "\b"用于定义BACKSPACE字符,即是有效的字符串转义序列。 PHP不支持 \b字符串转义序列,因此 "/\b/" = '/\b/'在那里。

All languages - Default flags - Global and Multiline-请注意,默认情况下,regex101.com上启用了 mg标志。因此,如果使用 ^$,它们将分别在行的开头和结尾匹配。如果在代码中需要相同的行为,请检查如何实现多行模式,并使用特定的标志,或者(如果支持的话)使用嵌入式 (?m)嵌入式(嵌入式)修饰符。 g标志启用多次匹配,通常使用特定的功能/方法来实现。检查您的语言参考以找到合适的语言参考。

-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]或其他替代方法。

-您正在处理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?

-请注意,您需要在模式周围使用正则表达式定界符,请参阅https://stackoverflow.com/questions/22430529

-您使用的 re.match仅在字符串开头搜索匹配项,请使用 re.searchRegex works fine on Pythex, but not in Python-如果正则表达式包含捕获组,则 re.findall返回捕获列表/捕获元组。使用非捕获组或 re.finditer,或删除冗余捕获组,请参见 re.findall behaves weird

-.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#

-使用原始字符串文字, RegExp(r"\d")或双反斜杠( RegExp("\\d"))-https://stackoverflow.com/questions/59085824

- 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 resultsRegExp#exec检索所有匹配项, RegEx to extract all matches from string using RegExp.exec-替换字符串中的所有模式匹配项: Why does javascript replace only first instance when using replace?

-如果您使用字符串文字定义正则表达式,或者仅使用正则表达式文字符号,则将反斜杠加倍,请参阅https://stackoverflow.com/questions/56097782

-单词边界不起作用?确保使用双反斜杠 "\\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?

- 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++

-双反斜杠或使用原始字符串文字: Regular expression doesn't work in Go-Go regex不支持环视,请在regex101.com上选择正确的选项( R"(\d)"),然后再进行测试! Regex expression negated set not working golang

-返回所有匹配项: Regex that works on regex101 does not work in Groovy

-字符串文字中的双转义反斜杠: "'\w' is an unrecognized escape" in grep-使用 Go来PCRE引擎( perl=TRUE / (g)sub): Why is this regex using lookbehinds invalid in R?

-所有量词的贪婪度由正则表达式中的第一个量词设置,请参见 Regex101 vs Oracle Regex(然后,您需要使所有量词与第一个量词一样贪婪)

-双转义反斜杠,确保 (g)regexpr仅出现在模式的开头,而 ^仅位于模式的末尾(如果有),并且请注意,您不能使用超过9个内联反向引用: Firebase Rules Regex Birthday

- $中的 /pattern/g必须不包含 REGEXP_REPLACE正则表达式定界符和标志(例如 /)-请参见 How to use Regex to replace square brackets from date field in Google Data Studio?

- Why does my regular expression work in X but not in Y?

- g[[:<:]]在正则表达式测试器中不起作用,尽管它们在PCRE中是有效的构造,请参见https://stackoverflow.com/questions/48670105

关于regex - 正则表达式适用于regex101.com,但不适用于prod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61124532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com