Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何通过正则表达式查找括号内的文本(有些例外)?_Ruby_Regex - Fatal编程技术网

Ruby 如何通过正则表达式查找括号内的文本(有些例外)?

Ruby 如何通过正则表达式查找括号内的文本(有些例外)?,ruby,regex,Ruby,Regex,我有一个regex/^\[(text:\s*+?\s*)\]/mi,它当前用于捕获以text:开头的括号中的文本。下面是一个它工作的示例: [text: here is my text that is captured within the brackets.] 现在,我想添加一个例外,以便允许某些括号,如以下情况: [text: here is my text that is captured within the brackets and also include ![](/some/pa

我有一个regex
/^\[(text:\s*+?\s*)\]/mi
,它当前用于捕获以
text:
开头的括号中的文本。下面是一个它工作的示例:

[text: here is my text that is
captured within the brackets.]
现在,我想添加一个例外,以便允许某些括号,如以下情况:

[text: here is my text that is
captured within the brackets
and also include ![](/some/path)]
基本上,我需要它来允许
![](/some/path)
匹配中的括号

任何帮助都将不胜感激。谢谢

更新:

以下是一些括号内的文本应匹配的情况:

[text: here is my text that is
captured within the brackets
and also include ![](/some/path)]

[text: here is my text that is
captured within the brackets
and also include ![](/some/path) and some more text]

[text: ![](/some/path)]

![text: cat]
以下是一些不应匹配的情况:

[text: here is my text that is
captured within the brackets
and also include ![invalid syntax](/some/path)]

[text: here is my text that is
captured within the brackets
and also include ![] (/some/path)]

[text: here is my text that is
captured within the brackets
and also include ! [](/some/path)]

[text: here is my text that is
captured within the brackets
and also include ! [] (/some/path)]

我认为您应该尝试以下正则表达式:

^\[(text:.*?(?<!\[))\]

^\[(文本:.*)(?确定,因此您希望允许

  • 不是括号或括号的字符
  • 序列
    ![]
在开始和结束括号之间。这将为您提供正则表达式

/^\[(text:[^\[\]]*(?:!\[\][^\[\]]*)*)\]/mi
说明:

^#行首
\[匹配[
(#开始捕获组
文本:#匹配文本:
[^\[\]*\\匹配除[或]之外的任意数量的字符
(?:#可选非捕获组:
!\[\]\匹配![]
[^\[\]*\\匹配除[或]之外的任意数量的字符
)*#根据需要重复(0次即可)
)#捕获组结束
\]#匹配]
测试它。

我在这个正则表达式中使用了一个来断言结束括号不会紧跟在开始括号之后:

^\[(text:.+?)(?<!\[)\]
^\[(文本:.+?)(?)?
这是步行通道

^#线锚的开始。
\[#匹配开口支架'['
(#开始捕捉第1组。
文本:#匹配“文本:”
.+?#延迟匹配任意字符一次或多次。
)#结束捕获第1组。
(?

这里有一个。

我不明白新行字符与您描述的内容有什么关系,所以我删除了
^

/\[(text:(?:[^\[\]]|!\[\][/\w]+)+)\]/i

您可以使用稍微修改和简化的正则表达式

str =<<_
[text: here is my text that is
captured within the brackets
and also includes ![](/some/path)]
and other stuff
_

r = /
    ^       # match beginning of string
    \[text: # match string
    .+?     # match one or more characters lazily
    \]      # match right bracket
   /imx      # case indifferent (i), multiline (m) and extended/free-spacing (x) modes

PLACEHOLDER = 0.chr
SUBSTITUTE_OUT = '![](/'

puts str.gsub(SUBSTITUTE_OUT, PLACEHOLDER).
  scan(r).
  map { |s| s.gsub(PLACEHOLDER, SUBSTITUTE_OUT) }

[text: here is my text that is
captured within the brackets
and also includes ![](/some/path)]

str=到底是什么让这些括号变得特别,它们应该被匹配?是它们前面的
吗?是它们是相应的开始/结束括号吗?这些括号可以嵌套得更深吗?正则表达式的什么味道?我们需要知道什么“味道”是的,是括号前的
使它与众不同。不确定它是什么味道的,但它是为RoR设计的。(rails 3.2,ruby 2.2)如果你不在正则表达式中使用
,你就不需要
m
标识符。@sawa:Ah,
ruby
标记是在我的答案之后添加的。幸运的是,
^
在ruby中的含义是明确的:)聪明!注意
\s*
没有添加任何内容。如果你在扩展模式下编写正则表达式(请参阅我的答案)你不必重复你自己(认识到扩展模式不能总是被使用).Nice;我使用冗长的Python编写文档/可读性,但不确定如何在Ruby中实现。关于不必要的
\s*
,我已经更新了我的答案。不要忘记,如果成员的名字不在注释中(例如@CarySwoveland或just@Cary),则不会通知成员指向他们的注释。