gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:47:38 29 4
gpt4 key购买 nike

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 中没有回溯 :(

但无论如何,我需要一个正则表达式来获得匹配的正确开始和结束索引。没有领先的空间。

最佳答案

确保您始终在 regex101.com 上选择正确的正则表达式引擎。请参阅由于使用 JS-only compatible regex with [^] construct in Python 而发生的问题。
JS 正则表达式 - 在回答这个问题时 - 不支持后视。现在,它在 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|^) ,然后匹配 @ ,然后匹配并捕获到 Group 1 一个或多个 0x23138 的非空白字符 0x23418
要获取开始/结束索引,请使用

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 上工作,但不在...
  • 首先 ,您是否检查了左侧工具 Pane 中的代码生成器链接? enter image description here
  • (\S+) - 确保在正则表达式测试器中针对代码中使用的相同文本(文字字符串)进行测试。一个常见的场景是将 string literal 值直接复制/粘贴到测试字符串字段中,所有字符串转义序列如 All languages - "Literal string" vs. "String literal" alert(换行符)、\n(回车)、0x231384tab)。例如,参见 Regex_search c++ 。请注意,它们必须替换为它们的字面对应物。因此,如果您在 Python 中使用 \r ,则必须在正则表达式测试器文本字段中使用 \t 、两个换行符、 text = "Text\n\n abc"Text will never match it 虽然你 might think it "works" 。是的,abc 并不总是匹配换行符,见 How do I match any character across multiple lines in a regular expression?
  • Text.*?abc - 确保你正确使用反斜杠在字符串中,在大多数语言中,规则字符串,使用双反斜线,即.在regex101.com使用必须写成All languages - Backslash alert。在原始字符串文字中,使用单个反斜杠,与 regex101 相同。逃离字边界是非常重要的,因为,在许多语言(C#PythonJavaJavaScriptRuby等),\d用于定义退格字符,即它是一个有效的字符串转义序列。 PHP 不支持 \\d 字符串转义序列,所以 "\b" = \b 那里。
  • "/\b/" - 请注意,默认情况下 0x2518122313431241 和 13431241 标志启用了 0x2518122313431241 和 13431241 1343124131431314131431818因此,如果您使用 '/\b/'All languages - Default flags - Global and Multiline ,它们将相应地在行的开头和结尾匹配。如果您在代码中需要相同的行为,请检查多行模式的实现方式并使用特定标志,或者 - 如果支持 - 使用内联 m 嵌入(内联)修饰符。 g 标志启用多次匹配,通常使用特定的函数/方法来实现。检查您的语言引用以找到合适的引用。
  • - regex101.com 上的行结尾 仅是 LF,你不能用 1432143 CR 结束测试字符串,见 1813225解决方案可以为每个正则表达式库不同:要么使用^(PCRE, java , ruby )或某种$(升压,PCRE),(?m)g/\R其他图书馆(好.NET)或\v(见答案regex101.com VS myserver - different results , C# )。与针对多行字符串(不是独立字符串/行列表)测试正则表达式这一事实相关的另一个问题是,您的模式可能会消耗行尾 \r?\n ,带有否定字符类的字符,请参阅 PHP(?:\r\n?|\n) 匹配行尾字符,为了避免它,可以使用 (?>\r\n?|\n) 或其他替代方法。
  • an issue like that - 你正在处理Unicode字符串,或想速记字符类匹配Unicode字符,太(如[\r\n]+匹配\n\D,或[^\d\n]匹配硬空格),那么你需要使用,看到 \w+ modifier - 要匹配所有出现的情况,使用 preg_match() returns 0 although regex testers work ,而不是 СтрибижевStribiżew ,见 \s+ 和 0x2518121423 返回 1518121423 与 0x2518121423 一样工作 1518121423 返回 1518121423您是否使用双引号字符串文字?使用单引号,见PHP preg_match to find multiple occurrences
  • "Unknown modifier 'g' in..." when using preg_match in PHP? Backreference does not work in PHP - 请注意模式周围需要正则表达式分隔符,请参阅 https://stackoverflow.com/questions/22430529
  • - 您使用u只搜索匹配的字符串,使用preg_match_all的开始: - 如果正则表达式中包含捕获组(S),preg_match返回的捕获/捕获的元组的列表。使用非捕获组,或 /...pattern.../g ,或删除多余的捕获组,请参阅 - 如果您在模式中使用 \1 来表示一行的开始,而不是整个字符串的开始,则不使用 de40x2318而不是一个字符串,通过re.matchre.search标志re.findall方法,请参阅Regex works fine on Pythex, but not in Python - 如果你尝试匹配多行一些文字,并使用re.finditer^,或$/re.M,仍然没有什么作品,检查,如果你读文件一行一行,比如说,用 re.MULTILINE 。您必须将整个文件内容作为输入传递给正则表达式方法,请参阅 re.findall behaves weird
  • Using ^ to match beginning of line in Python regexGetting Everything Between Two Characters Across New Lines - .NET正则表达式不支持占有欲量词像rere.DOTALLre.S[\s\S]*,看到 - 当你对阵一个多行字符串,并在模式中使用[\s\S]*?选项(或内联for line in file:改性剂)与++ anchor 匹配整行,并获得代码不匹配,则需要*+前加??,看到 - 要获得多个匹配,使用{1,10}?,不RegexOptions.Multiline,看到.NET regex matching digits between optional text with possessive quantifer is not working - 类似情况如上:将字符串分割成段,由双换行符序列 - .Net regex matching $ with the end of the string and not of line, even with multiline enabled - 您应该删除正则表达式分隔符,即 (?m) 实际上必须看起来像 $ ,请参阅 0x2518122133412323334123233341232333412323341241232334123之间的所有字符。正则表达式(如 \r? )你需要删除 $ ,见 RegEx Match multiple times in string
  • C# / Regex Pattern works in online testing, but not at runtime Simple and tested online regex containing regex delimiters does not work in C# code - 使用原始字符串文字、Regex.Matches 或双反斜杠 (0x251843125)/stackover13341125/stack18412485134128
  • Regular Expression working in regex tester, but not in c# - Regex.Match 中的双转义反斜杠:
    - (负)lookbehinds大多数浏览器不支持: - 字符串是不可改变的,在@"/\d+/"结果赋值给一个变种 - - 检索所有比赛用@"\d+" - Why do regex constructors need to be double escaped?或与Regex.EscapeRegex works on browser but not in Node.js - 字符串替换所有模式匹配:The .replace() method does change the string in place
  • Regex101 and Js regex search showing different results RegEx to extract all matches from string using RegExp.exec - 如果您使用字符串文字定义正则表达式,或者只使用正则表达式文字表示法,请将反斜杠加倍,请参阅 https://stackoverflow.com/0977826
  • Why does javascript replace only first instance when using replace? - 字边界不起作用?确保使用双反斜杠 Regex.Escape(@"\d+\.\d+") ,请参阅 - 获取 Regex.Escape 异常?同样的事情,双反斜杠 - - RegExp(r"\d") 在打扰你吗?运行RegExp("\\d")/RegExp("\\d") - - .replace需要一个完整的字符串匹配,使用str.match(/pat/g):Regex \b word boundary not works - 使用RegExp#exec存取组:Java doesn't work with regex \s, says: invalid escape sequence - 在字符类,既"\\b"invalid escape sequence必须进行转义 - Why does my regex work on RegexPlanet and regex101 but not in my code? - 你不应该运行No match foundMatcher.find()连续,仅使用 Matcher.matches() 检查模式是否与整个字符串匹配,然后执行相应操作,或使用 .matches() 检查是否存在单个匹配或 .find() 以查找多个匹配(或 0x2518411242)。见 Java Regex pattern that matches in any online tester but doesn't in Eclipse
  • Regex not working in Java while working otherwise - 你有 matcher.group(x) 吗?删除外斜线,它们是 Using square brackets inside character class in Java regex 不是模式的一部分。请参阅 [ - 您期望部分字符串匹配,但 ] 需要完整字符串匹配?使用 matcher.matches() ,见 Why does my regex work on RegexPlanet and regex101 but not in my code?
  • - 不要用单/双引号将 matcher.find() 括起来,见 regex delimiter chars
  • Find one or more word in string using Regex in Kotlin - if (matcher.matches()) {...}需要一个完整的字符串匹配,使用if (matcher.find())找到部分匹配 - Regex doesn't match in Kotlin - while (matcher.find())只找到了第一场比赛。使用Matcher#results()Regex("/^\\d+$/")得到所有的比赛:看 - 当你阅读使用.matchEntire用户自定义字符串,请注意.find只会在第一空白,以正确读取整条生产线,使用/.../ - mongodb regex doesn't work - regex_match不工作,您需要使用 regex_searchregex_search(原始字符串文字) - - 确保针对文字而不是字符串文字测试正则表达式,请参阅 0x2421341211
  • Regex not working as expected with C++ regex_match - 双反斜杠或使用原始字符串文字:What does std::match_results::size return? - Go regex 不支持环视,在测试之前选择正确的选项( 0x251812.com4113 之前的 0x2518124113) C++ Regex to match '+' quantifier
  • This regex doesn't work in c++ - 返回所有匹配项:Regex_search c++
  • - 双反斜线逃逸的字符串常量:Regular expression doesn't work in Go - 使用sregex_token_iterator到PCRE引擎(sregex_iterator/std::string input; std::cin >> input;):Regex expression negated set not working golang
  • - 所有量词的贪婪性由正则表达式中的第一个量词设置,参见 Regex that works on regex101 does not work in Groovy(然后,您需要将所有量词设为第一个)贪婪
  • - 双转义反斜杠,确保 cin 只出现在模式的开头和 std::getline(std::cin, input); 只出现在末尾,如果 201343141 位于后面,如果 2014 和 24 之间的任何一个都不能使用,请注意 18 和 24 的末尾,如果 24 和
  • "'\w' is an unrecognized escape" in grep Why is this regex using lookbehinds invalid in R? - 在 Firestore 安全规则中,正则表达式需要作为字符串传递,这也意味着它不应该被包裹在 0x2518122312123 符号中,使用 0x2518122312123 145 138 145 145 145 145 145 145 145 145 145 14 14 13 13 13 15/63243300
  • - "\d""\\d"不能包含R"(\d)"正则表达式的分隔符和标志(像Go) - 见Regex101 vs Oracle Regex
  • - 如果你认为 perl=TRUE 没有返回完全匹配,截断结果,你应该检查你的正则表达式中是否有多余的捕获组并删除它们,或者将捕获组转换为非开放捕获组见 Firebase Rules Regex Birthday
  • -
  • How to use Regex to replace square brackets from date field in Google Data Studio? - (g)sub(g)regexpr不工作的正则表达式测试仪,虽然他们是在PCRE有效的结构,看https://stackoverflow.com/questions/48670105
  • 关于regex - 正则表达式适用于 regex101.com,但不适用于 prod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39636124/

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