简体   繁体   English

正则表达式匹配字符串,除非后跟#

[英]regex match string unless followed by #

I'm trying to add a #param to url's I want to add it to all urls that doesn't already have the # to avoid double param. 我想将#param添加到url中,我想将其添加到所有尚未包含#的url中,以避免双重参数。 My urls doesn't look like urls they are made up of handlebar parameters. 我的网址看起来不像是由车把参数组成的网址。

they can look like following: 它们可能如下所示:

{{app.url}}
{{root.app.url}}
{{app.url}}#param
{{root.app.url}}#param

So I came up with a regex that matches the handlebar tag ({{(root.)?app.url}}) 因此,我想出了一个与车把标签({{(root。)?app.url}})相匹配的正则表达式

only problem is that when I later uses regexp_replace(url, '({{(root\\.)?app\\.url}})', '\\1#param') 唯一的问题是,当我以后使用regexp_replace(url, '({{(root\\.)?app\\.url}})', '\\1#param')

my result looks like this: 我的结果看起来像这样:

{{app.url}}#param
{{root.app.url}}#param
{{app.url}}#param#param
{{root.app.url}}#param#param

One solution I can think of is doing it in two steps, and the 2nd step should look for duplicate #param#param and replace that with single #param. 我可以想到的一种解决方案是分两个步骤进行,第二步应该查找重复的#param#param并将其替换为单个#param。

But it had me wondering if there was a way using regex to exclude the handlebar tags that are followed by # and completely cancel that match? 但这让我想知道是否存在使用正则表达式排除#后面的车把标签并完全取消该匹配的方法?

Here are some examples: https://regex101.com/r/d3Zyvo/6 以下是一些示例: https : //regex101.com/r/d3Zyvo/6

Note: this is for use in postgressql update queries. 注意:这是在postgressql更新查询中使用的。 The regex is POSIX/PCRE. 正则表达式为POSIX / PCRE。 I must use regex_replace with back reference since there might be content before and after the hanbdlebar tags, I simply cannot just concatenate the param. 我必须将regex_replace与反向引用一起使用,因为在hanbdlebar标记之前和之后可能都有内容,我无法简单地将参数串联起来。 (see the link). (请参阅链接)。

You may use a negative lookahead (?!#) : 您可以使用负数前瞻(?!#)

({{(root\.)?app\.url}})(?!#)
                       ^^^^^ 

See the regex demo . 参见regex演示

Details 细节

  • ({{(root\\.)?app\\.url}}) - Group 1 (later referred to with \\1 from the replacement pattern): ({{(root\\.)?app\\.url}}) -组1(在替换模式中后来用\\1 ):
    • {{ - {{ substring {{ - {{子字符串
    • (root\\.)? - an optional Group 2 matching 1 or 0 occurrences of root. -匹配1或0 root.出现的可选第2组root.
    • app\\.url}} - a literal app.url}} substring app\\.url}} -文字app.url}}子字符串
  • (?!#) - a negative lookahead that fails the match if, immediately to the right of the current location, there is a # char. (?!#) -如果当前位置右侧紧邻有#字符,则匹配失败的否定前行。

See Table 9-17. 请参阅表9-17。 Regular Expression Constraints : 正则表达式约束

(?!re) negative lookahead matches at any point where no substring matching re begins (AREs only) (?!re)否定超前匹配在没有重新开始子字符串匹配的任何点(仅适用于ARE)

PostgreSQL demo: PostgreSQL演示:

select regexp_replace('{{app.url}}
{{root.app.url}}
{{app.url}}#param
{{root.app.url}}#param',
                      '({{(root\.)?app\.url}})(?!#)',
                      '\1#param',
                      'g');

在此处输入图片说明

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

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