标签: regex-greedy

如何使用正则表达式匹配字符串中的第 n 次出现

如何使用正则表达式匹配字符串中的第 n 次出现

set test {stackoverflowa 是查找站点的最佳解决方案 stackoverflowb 是查找站点的最佳解决方案 stackoverflowc 是查找站点的最佳解决方案stackoverflowd 是查找站点的最佳解决方案stackoverflowe 是查找站点的最佳解决方案}

regexp -all {stackoverflow} $test 
Run Code Online (Sandbox Code Playgroud)

上面的一个给出“5”作为输出

regexp {stackoverflow} $test 
Run Code Online (Sandbox Code Playgroud)

上面的结果给出了stackoverflow,这里它匹配stackoverflow的第一次出现(即)stackoverflowa

我的要求是我想从上面给定的字符串中匹配第 5 次出现的 stackoverflow(即)stackoverflowe。

请有人澄清我的问题..谢谢

然后又一个问题

regex tcl regex-greedy

5
推荐指数
1
解决办法
4181
查看次数

将正则表达式与可选的前瞻匹配

我有以下字符串:

NAME John Nash FROM California

NAME John Nash

我想要一个能够为两个字符串提取"John Nash"的正则表达式.

这是我试过的

"NAME(.*)(?:FROM)"
"NAME(.*)(?:FROM)?"
"NAME(.*?)(?:FROM)?"
Run Code Online (Sandbox Code Playgroud)

但这两个字符串都不起作用.

python regex regex-greedy regex-lookarounds

5
推荐指数
1
解决办法
1295
查看次数

如何将大括号中的文本分配到vim中的变量中

有没有办法在某个乳胶命令(\ body)之前用大括号获取数据并将该内容(长文本)分配给变量.

例如:

\ text {只是身体前的文字}\body {包含很多段落等等等等,而且这些段落还包含很多乳胶命令,如\ textbf {my name}和\ textit {text}等,但我想要所有的内容在括号中}\text {只是身体后的文字}

我需要

\ body {包含很多段落等等,而且段落中还包含很多乳胶命令,比如\ textbf {my name}和\ textit {text}等,但我希望变量中的所有内容都在一个变量中

我想要一些搜索并替换它.这就是为什么

我做了一个宏来在%的帮助下用括号拉出文本(用括号搜索).

有没有简单的方法来做到这一点?

提前致谢

regex vim vim-plugin regex-greedy

5
推荐指数
1
解决办法
158
查看次数

将除 <pre> 标签内以外的换行符替换为 <pre> 标签内的括号 (<>)

我使用问题中可用的答案替换了预标记之外的所有换行符。

\n(?![^<]*<\/pre>)
Run Code Online (Sandbox Code Playgroud)

它工作正常,直到预标记中的内容有 < 或 > 括号。

例如,输入:

<p>Test contennt for regex
with line breaks</p>
<pre>code block 
with multi line content
working fine</pre>
<pre class="brush:C#">
test line break before 
open paranthesis < is not working fine
line breaks after paranthesis
is accepted
</pre>
Run Code Online (Sandbox Code Playgroud)

输出是

<p>Test contennt for regexwith line breaks</p><pre>code block 
with multi line content
working fine</pre><pre class="brush:C#">test line break before open paranthesis < is not working fine
line breaks after paranthesis
is accepted
</pre>
Run Code Online (Sandbox Code Playgroud)

这是不正确的 - 并非所有换行符都被删除。

请参阅 …

regex regex-negation regex-greedy

5
推荐指数
1
解决办法
192
查看次数

正则表达式用于匹配仅由字母列表构成的单词

给定一组单词,我需要知道哪些单词仅由一组字母组成。即使此字母是验证集的一部分,该单词的字母也不能超过允许的数量。

例:

Char set: a, a, ã, c, e, l, m, m, m, o, o, o, o, t (fixed set)

Words set: mom, ace, to, toooo, ten, all, aaa (variable set)
Run Code Online (Sandbox Code Playgroud)

结果:

mom = true
ace = true
to = true
toooo = true
ten = false (n is not in the set)
all = false (there is only 1 L in the set)
aaa = false (theres is only 2 A in the set)
Run Code Online (Sandbox Code Playgroud)

如何在Javascript中生成此正则表达式?(区分大小写不是问题)。

我尝试了以下代码,但未成功:

var str = …
Run Code Online (Sandbox Code Playgroud)

javascript regex regex-group regex-greedy regex-lookarounds

5
推荐指数
1
解决办法
113
查看次数

匹配日期的正则表达式(月日、年或 m/d/yy)

我正在尝试编写一个正则表达式,该表达式可用于在字符串中查找日期,该字符串前面(或后面)可能有空格、数字、文本、行尾等。该表达式应处理美国日期格式要么

1) Month Name Day, Year - 即 2019 年 1 月 10 日或
2) mm/dd/yy - 即 11/30/19

我为月份名称,年份找到了这个

(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4}
Run Code Online (Sandbox Code Playgroud)

(感谢 Veverke 在这里Regex 匹配日期,如月份名称日逗号和年份

这对于 mm/dd/yy(以及 m/d/y 的各种组合)

(1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2} 
Run Code Online (Sandbox Code Playgroud)

(在此感谢 Steven Levithan 和 Jan Goyvaerts https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s04.html

我试图把它们像这样结合起来

((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4})|((1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2})
Run Code Online (Sandbox Code Playgroud)

当我在输入字符串“Paid on 1/1/2019”中搜索“on [regex above]”时,它确实找到了日期,但没有找到“on”这个词。如果我只是使用,则找到该字符串

(1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2}
Run Code Online (Sandbox Code Playgroud)

谁能看到我做错了什么?

编辑

我正在使用下面的 c# .net 代码:

    string stringToSearch = "Paid on 1/1/2019";
    string searchPattern = @"on ((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4})|((1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2})";
    var match = Regex.Match(stringToSearch, searchPattern, RegexOptions.IgnoreCase);


    string foundString;
    if (match.Success)
        foundString= stringToSearch.Substring(match.Index, match.Length);
Run Code Online (Sandbox Code Playgroud)

例如

string searchPattern = …
Run Code Online (Sandbox Code Playgroud)

c# regex regex-group regex-greedy regex-lookarounds

5
推荐指数
1
解决办法
5608
查看次数

swift中的正则表达式

我对 swift 中的 NSRegularExpression 有点困惑,有人可以帮助我吗?

任务:1给出("name","john","name of john")
那么我应该得到["name","john","name of john"]. 在这里我应该避免使用括号。

任务:2给出("name"," john","name of john")
那么我应该得到["name","john","name of john"]. 在这里我应该避免括号和额外的空格,最后得到字符串数组。

任务:3给出key = value // comment
那么我应该得到["key","value","comment"]. 在这里,我应该通过避免只获取行中的字符串,=并且//
我已经尝试了下面的任务 1 代码但没有通过。

let string = "(name,john,string for user name)"
let pattern = "(?:\\w.*)"

do {
    let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive)
    let matches = regex.matches(in: string, options: [], range: NSRange(location: 0, length: string.utf16.count))
    for match …
Run Code Online (Sandbox Code Playgroud)

regex regex-group nsregularexpression regex-greedy swift

5
推荐指数
1
解决办法
3900
查看次数

RegEx用于提取HTML标签中的特定textContent

我需要创建一个Python程序,该程序从标准输入接收HTML文件,并使用regext将哺乳动物下显示的物种名称逐行输出到标准输出。我也不需要输出显示为“ #sequence_only”的项目。

用于标准输入的文件是这样的:

   <!DOCTYPE html>

  <!-- The following setting enables collapsible lists -->
  <p>
  <a href="#human">Human</a></p>

  <p class="collapse-section">
  <a class="collapsed collapse-toggle" data-toggle="collapse" 
  href=#mammals>Mammals</a>
  <div class="collapse" id="mammals">
  <ul>
  <li><a href="#alpaca">Alpaca</a>
  <li><a href="#armadillo">Armadillo</a>
  <li><a href="#sequence_only">Armadillo</a> (sequence only)
  <li><a href="#baboon">Baboon</a>
  <li><a href="#bison">Bison</a>
  <li><a href="#bonobo">Bonobo</a>
  <li><a href="#brown_kiwi">Brown kiwi</a>
  <li><a href="#bushbaby">Bushbaby</a>
  <li><a href="#sequence_only">Bushbaby</a> (sequence only)
  <li><a href="#cat">Cat</a>
  <li><a href="#chimp">Chimpanzee</a>
  <li><a href="#chinese_hamster">Chinese hamster</a>
  <li><a href="#chinese_pangolin">Chinese pangolin</a>
  <li><a href="#cow">Cow</a>
  <li><a href="#crab-eating_macaque">Crab-eating_macaque</a>
  <div class="gbFooterCopyright">
  &copy; 2017 The Regents of the University of California. All 
  Rights Reserved.
  <br>
  <a …
Run Code Online (Sandbox Code Playgroud)

html python regex regex-group regex-greedy

5
推荐指数
0
解决办法
121
查看次数

令人惊讶但正确的贪婪子表达式在积极的后视断言中的行为

注意

  • 观察到的行为是正确的,但起初可能令人惊讶;对我来说是这样,我认为对其他人也可能是这样——尽管对那些非常熟悉正则表达式引擎的人可能不是这样。

  • 重复建议的重复项Regex lookahead、lookbehind 和 atomic groups包含有关环视断言的一般信息,但没有解决手头的具体误解,如下面的评论中更详细地讨论。


使用greedy,根据定义,在肯定的后视断言中的可变宽度子表达式可以表现出令人惊讶的行为。

为了方便起见,这些示例使用 PowerShell,但该行为通常适用于 .NET 正则表达式引擎:

这个命令按我直觉的预期工作:

# OK:  
#     The subexpression matches greedily from the start up to and
#     including the last "_", and, by including the matched string ($&) 
#     in the replacement string, effectively inserts "|" there - and only there.
PS> 'a_b_c' -replace '^.+_', '$&|'
a_b_|c
Run Code Online (Sandbox Code Playgroud)

下面的命令,该命令使用正向后看断言,(?<=...)看似等价-但不是: …

.net regex regex-greedy regex-lookarounds

5
推荐指数
1
解决办法
260
查看次数

为什么正则表达式量词 {n,} 比 + (在 Python 中)更贪婪?

我尝试使用正则表达式来查找由重复的双字母形成的最大长度序列,例如AABBstring 中的xAAABBBBy

正如官方文档中所述:

'*''+''?'量词都是贪婪的;它们匹配尽可能多的文本。

当我使用量词时{n,},我得到一个完整的子字符串,但+仅返回部分:

import re

print(re.findall("((AA|BB){3,})", "xAAABBBBy"))
# [('AABBBB', 'BB')]
print(re.findall("((AA|BB)+)", "xAAABBBBy"))
# [('AA', 'AA'), ('BBBB', 'BB')]
Run Code Online (Sandbox Code Playgroud)

为什么{n,}比 更贪婪+

python regex regex-greedy

5
推荐指数
1
解决办法
143
查看次数