简体   繁体   中英

Adding exceptions to regexp patern in ruby

I wrote this regexp to convert a string into an HTML tag. It matches [img foo] and a third optional ( left or right ) argument at the end. For example, [img foo left] .

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

But it also matches these tags inside markdown inline code and code blocks. 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

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 . With it, you can put each piece of regex in a (els)if condition under separate cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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