- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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+)/
最佳答案
确保您始终在 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);
(\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#,Python,Java,JavaScript,Ruby等),\d
用于定义退格字符,即它是一个有效的字符串转义序列。 PHP 不支持 \\d
字符串转义序列,所以 "\b"
= \b
那里。"/\b/"
- 请注意,默认情况下 0x2518122313431241 和 13431241 标志启用了 0x2518122313431241 和 13431241 1343124131431314131431818因此,如果您使用 '/\b/'
和 All languages - Default flags - Global and Multiline
,它们将相应地在行的开头和结尾匹配。如果您在代码中需要相同的行为,请检查多行模式的实现方式并使用特定标志,或者 - 如果支持 - 使用内联 m
嵌入(内联)修饰符。 g
标志启用多次匹配,通常使用特定的函数/方法来实现。检查您的语言引用以找到合适的引用。^
(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)
或其他替代方法。[\r\n]+
匹配\n
或\D
,或[^\d\n]
匹配硬空格),那么你需要使用php,看到 \w+
modifier - 要匹配所有出现的情况,使用 preg_match() returns 0 although regex testers work ,而不是 Стрибижев
和 Stribiżew
,见 \s+
和 0x2518121423 返回 1518121423 与 0x2518121423 一样工作 1518121423 返回 1518121423您是否使用双引号字符串文字?使用单引号,见PHP preg_match to find multiple occurrencesu
只搜索匹配的字符串,使用preg_match_all
的开始:laravel - 如果正则表达式中包含捕获组(S),preg_match
返回的捕获/捕获的元组的列表。使用非捕获组,或 /...pattern.../g
,或删除多余的捕获组,请参阅 python - 如果您在模式中使用 \1
来表示一行的开始,而不是整个字符串的开始,则不使用 de40x2318而不是一个字符串,通过re.match
或re.search
标志re.findall
方法,请参阅Regex works fine on Pythex, but not in Python - 如果你尝试匹配多行一些文字,并使用re.finditer
或^
,或$
/re.M
,仍然没有什么作品,检查,如果你读文件一行一行,比如说,用 re.MULTILINE
。您必须将整个文件内容作为输入传递给正则表达式方法,请参阅 re.findall behaves weird 。re
,re.DOTALL
,re.S
,[\s\S]*
,看到c# - 当你对阵一个多行字符串,并在模式中使用[\s\S]*?
选项(或内联for line in file:
改性剂)与++
anchor 匹配整行,并获得代码不匹配,则需要*+
前加??
,看到.net - 要获得多个匹配,使用{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 stringRegex.Matches
或双反斜杠 (0x251843125)/stackover13341125/stack18412485134128Regex.Match
中的双转义反斜杠:dart@"/\d+/"
结果赋值给一个变种 - javascript - 检索所有比赛用@"\d+"
- Why do regex constructors need to be double escaped?或与Regex.Escape
,Regex works on browser but not in Node.js - 字符串替换所有模式匹配:The .replace() method does change the string in placeRegex.Escape(@"\d+\.\d+")
,请参阅 javascript - 获取 Regex.Escape
异常?同样的事情,双反斜杠 - angular - RegExp(r"\d")
在打扰你吗?运行RegExp("\\d")
/RegExp("\\d")
- java - .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 found
和Matcher.find()
连续,仅使用 Matcher.matches()
检查模式是否与整个字符串匹配,然后执行相应操作,或使用 .matches()
检查是否存在单个匹配或 .find()
以查找多个匹配(或 0x2518411242)。见 Java Regex pattern that matches in any online tester but doesn't in Eclipsematcher.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 charsif (matcher.matches()) {...}
需要一个完整的字符串匹配,使用if (matcher.find())
找到部分匹配 - Regex doesn't match in Kotlin - while (matcher.find())
只找到了第一场比赛。使用Matcher#results()
或Regex("/^\\d+$/")
得到所有的比赛:看mongodb - 当你阅读使用.matchEntire
用户自定义字符串,请注意.find
只会在第一空白,以正确读取整条生产线,使用/.../
- mongodb regex doesn't work - regex_match
不工作,您需要使用 regex_search
或 regex_search
(原始字符串文字) - c++ - 确保针对文字而不是字符串文字测试正则表达式,请参阅 0x2421341211sregex_token_iterator
到PCRE引擎(sregex_iterator
/std::string input; std::cin >> input;
):Regex expression negated set not working golangcin
只出现在模式的开头和 std::getline(std::cin, input);
只出现在末尾,如果 201343141 位于后面,如果 2014 和 24 之间的任何一个都不能使用,请注意 18 和 24 的末尾,如果 24 和"\d"
在"\\d"
不能包含R"(\d)"
正则表达式的分隔符和标志(像Go
) - 见Regex101 vs Oracle Regexperl=TRUE
没有返回完全匹配,截断结果,你应该检查你的正则表达式中是否有多余的捕获组并删除它们,或者将捕获组转换为非开放捕获组见 Firebase Rules Regex Birthday(g)sub
和(g)regexpr
不工作的正则表达式测试仪,虽然他们是在PCRE有效的结构,看https://stackoverflow.com/questions/48670105关于regex - 正则表达式适用于 regex101.com,但不适用于 prod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39636124/
我正在尝试编写一个函数,该函数接受输入字符串、正则表达式(由 std.regex.regex 从原始字符串生成)和错误消息字符串,并尝试匹配来自使用正则表达式输入字符串,如果没有匹配则显示错误消息。到
-edit- 注意 ?末.{2,}? 我发现你可以写 .{2,}? 是不是和下面一模一样? .{2} 最佳答案 号{2,}表示两次或更多次同时 {2}意思是正好两次。量词默认是贪婪的,所以给定字符串
我有以下文字: This is a test ::a. MODE 3 within 7 hours, ::b. MODE 4 within 13 hours, and ::c. MODE 5 with
我用 Regex.fromLiteral(".*") 创建了一个非常简单的匹配所有正则表达式. 根据documentation :“返回指定文字字符串的文字正则表达式。” 但是我真的不明白“对于指定的
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
该Web项目将静态内容放入一些/content/img文件夹中。 网址规则是:/img/{some md5} 但在文件夹中的位置:/content/img/{前两位数字}/ 例子 url:
我有以下数据: SOMEDATA .test 01/45/12 2.50 THIS IS DATA 我想从中提取数字 2.50。我已设法使用以下 RegEx 做到这一点: (?<=\d{2}\/\d{
我需要证明或反驳下面的正则表达式 (RS + R )* R = R (SR + R)* // or, for programmers: /(RS|R)*R/ == /R(SR|R)*/ 我有一种强烈的
对于具有自由文本的字符串: "The shares of the stock at the XKI Market fell by €89.99 today, which saw a drop of a
例如,我有 RegEx DSX-?2 的 var 我需要将此变量添加到 RegEx 并获取此 .match(/DSX-?2/gi) 最佳答案 您可以创建一个 RegExp对象使用 new RegExp
我无法区分大小写的搜索无法在SQLITE中用于REGEX。支持语法吗? SELECT * FROM table WHERE name REGEXP 'smith[s]*\i' 我希望得到以下答案(假设
Visual Studio / XPath / RegEx: 给定表达式: (?(Car|Car Blue)) +(?.+) +---> +(?.+) 给定搜索字符串: Car Blue Flying
我有一个看起来像这样的正则表达式 /^(?:\w+\s)*(\w+)$*/ 什么是?: ? 最佳答案 它表示子模式是非捕获子模式。这意味着在 (?:\w+\s) 中匹配的任何内容,即使它被 () 括起
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我在 Excel 工作表(也以 csv 格式)中获得了姓名列表,并根据姓名来源进行了分组。 这就是我创建的组的样子。 现在我想添加一个新列,名称后面包含组名称。 这就是我想要获得的。 我如何得到这个?
我试图将一个字符串拆分为一个字符串列表,单词是分开的,但是周围的字符,例如.. "?()“”!"也分开。 要分隔的字符串是"testing “testing” “one two three” (hi
我有一个来自视频转换文件的完整日志,它看起来像这样: -------------------------------------------------------------------------
在定界符为“-”的模式 X-Y-Z 中,我想检查 Y 是否具有大小 8 而没有重复。 Y 可以是像 Y = (A-B-C) 这样的子集,但如果没有,则 Y 的值为 1 1 - num-12345678
Java确实有这个功能,谢谢你的回答,对我来说失去对API的关注太可惜了... 例如: String strOriginal = "A:B&C@D"; 我认为java中应该有一个非常好的方法来改变它,
我只需要接受符合这些规则的输入... 0.25-24 0.25 的增量(.00、.25、.50、.75) 第一个数字不是必须的。 希望尾随零是可选的。 一些有效条目的示例: 0.25 .50 .5 1
我是一名优秀的程序员,十分优秀!