如何通过正则表达式在括号内查找文本?

时间:2015-11-18 06:35:13

标签: ruby 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)]

5 个答案:

答案 0 :(得分:6)

好的,所以你想要允许

  • 不是括号或
  • 的字符
  • 序列var winston = require('winston'); var fs = require( 'fs' ); var path = require('path'); var logDir = 'log'; // directory path you want to set if ( !fs.existsSync( logDir ) ) { // Create the directory if it does not exist fs.mkdirSync( logDir ); } var logger = new (winston.Logger)({ transports: [ new (winston.transports.Console)({ colorize: 'all' }), new (winston.transports.File)({filename: path.join(logDir, '/log.txt')}) ] }); logger.info("Anything you want to write in logfile");

在起始和结束括号之间。这给你正则表达式

![]

<强>解释

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

测试live on regex101.com

答案 1 :(得分:4)

您可以使用正则表达式,稍加修改和简化。

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)]

请注意,在正则表达式中,\s*.+?\s*.+?相同,并且(如@sawa所述)您可以将.+?替换为[^\]]+,在这种情况下,您可以不需要多线模式。

编辑:我根据OP编辑的问题更新了SUBSTITUTE_OUT。这说明了这种方法的一个优点:正则表达式不受内部匹配文本更改的影响。

答案 2 :(得分:3)

我在此正则表达式中使用了negative lookbehind来断言结束括号不会紧跟在左括号中:

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

这是演练。

^           # Start of line anchor.
\[          # Match opening bracket '['
(           # Start capturing group 1. 
text:       # Match 'text:'
.+?         # Match any character one or more times lazily.
)           # End capturing group 1. 
(?<!        # Begin negative lookbehind.
\[          # '[' must not preceed the next match.
)           # End negative lookbehind.
\]          # Match closing bracket.

这里是demo

答案 3 :(得分:3)

我不明白新线字符与您描述的内容有何关联,因此我删除了^

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

答案 4 :(得分:0)

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

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