[英]Adding exceptions to regexp patern in ruby
我编写了此正则表达式,将字符串转换为HTML标记。 它与[img foo]
和最后的第三个可选参数( left或right )匹配。 例如, [img foo left]
。
/\[img (\S+)(\sleft|\sright)?\]/
但它也与markdown内联代码和代码块内的这些标签匹配。 所以
````
[img foo] # matches, but should not (it's inside a markdown code block
````
`[img foo]` # matches but should not match (inline code)
我在提取引用时遇到了同样的问题。 这是完整的方法:
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
我想知道是否有添加异常或添加转义序列的方法? 解决此问题的最佳方法?
这是Rubular游乐场: http ://rubular.com/r/b9ClAE6Rhj
如果您将引号与标签的优先级匹配,则可以避免匹配引号内的标签。
quoted = /(?=```[^`]*```|`[^`]*`)/m
tagged = /\[img (\S+)(\sleft|\sright)?\]/
text.gsub(Regexp.union(quoted, tagged)) do
if $1 then "" else
...
end
end
或者,如果要避免正则表达式变得复杂,则应使用StringScanner
。 有了它,您可以将单独的正则表达式放在(els)if
条件下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.