简体   繁体   English

在Ruby中向regexp模式添加异常

[英]Adding exceptions to regexp patern in ruby

I wrote this regexp to convert a string into an HTML tag. 我编写了此正则表达式,将字符串转换为HTML标记。 It matches [img foo] and a third optional ( left or right ) argument at the end. 它与[img foo]和最后的第三个可选参数( leftright )匹配。 For example, [img foo left] . 例如, [img foo left]

/\[img (\S+)(\sleft|\sright)?\]/

But it also matches these tags inside markdown inline code and code blocks. 但它也与markdown内联代码和代码块内的这些标签匹配。 So 所以

````
  [img foo] # matches, but should not (it's inside a markdown code block
````
`[img foo]` # matches but should not match (inline code)

I'm having the same problem with fetching references. 我在提取引用时遇到了同样的问题。 Here is the full method: 这是完整的方法:

  def custom_image_tag(text)

    # look for image tag
    text.gsub(/\[img (\S+)(\sleft|\sright)?\]/) do
      id, css = $1, $2

      # check is second argument is a link
      # if yes use it in image tag
      if id =~ /http(s)?:\/\//
        image_tag id.strip, class: css

      # if no search doc to see if its value matches a reference
      # For example, [img foo] will match "[foo]: whatever.com"
      else
        text.match(/\[(#{id})\]: (.*)/) do |match|  # Same issue here
          image_tag match[2].strip, class: css
        end
      end
    end
  end

I wonder, is there a way to add an exception, or add an escape sequence? 我想知道是否有添加异常或添加转义序列的方法? Best way to solve this? 解决此问题的最佳方法?

Here's a Rubular playground: http://rubular.com/r/b9ClAE6Rhj 这是Rubular游乐场: http ://rubular.com/r/b9ClAE6Rhj

If you match the quotes with priority over the tag, then you can avoid matching the tags within quotes. 如果您将引号与标签的优先级匹配,则可以避免匹配引号内的标签。

quoted = /(?=```[^`]*```|`[^`]*`)/m
tagged = /\[img (\S+)(\sleft|\sright)?\]/
text.gsub(Regexp.union(quoted, tagged)) do
  if $1 then "" else
    ...
  end
end

Or, if you want to avoid the regex becoming complicated, then you should use StringScanner . 或者,如果要避免正则表达式变得复杂,则应使用StringScanner With it, you can put each piece of regex in a (els)if condition under separate cases. 有了它,您可以将单独的正则表达式放在(els)if条件下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM