python 正则表达式 前瞻_扩展此字符前瞻性正则表达式也可以使用字符串(Expanding on this character lookahead regex to also work with ...

扩展此字符前瞻性正则表达式也可以使用字符串(Expanding on this character lookahead regex to also work with a string)

我正在使用正则表达式从abbr对象中提取unix时间:

10:45

使用正则表达式,我已经学会了如何使用双引号进行负面和正面的前瞻:

(?!")[0-9].*?(?=") // returns 1468050300

这项工作正常,但现在想知道如何更具体地进行匹配?

例如,不只是对第一个“(双引号)做一个负面的预测,而且还包括字符串'data-utime ='

如果我想要utime但属性不再是第一组引号:

10:45

然后这个正则表达式不再有效。

I am using regex to pull out a unix time from an abbr object:

10:45

With regex I have learned how to do it with a negative and positive lookahead using the double quotes:

(?!")[0-9].*?(?=") // returns 1468050300

This works ok, but wonder now how would I go about matching more specifically?

e.g. not just doing a negative lookahead on the first "(double quote) but also including the string 'data-utime='

If I want the utime but the attribute is no longer the first set of quotes:

10:45

Then this regex no longer works.

原文:https://stackoverflow.com/questions/38259081

更新时间:2019-12-17 03:00

最满意答案

是否有任何理由不能使用与名称属性data-utime匹配的正则表达式,例如

data-utime="(.*?)"

这对于改变标签内相对位置的属性是稳健的,您不必处理负向和正向前瞻。

如果您发现自己必须编写非常复杂的正则表达式来解析XML,那么您应该认真考虑使用XML解析器,这是一个更好的工具。

演示:

Regex101

Is there any reason you can't use a regex which matches the attribute data-utime by name, e.g.

data-utime="(.*?)"

This would be robust to the attribute changing relative position within the tag, and you would not have to deal with negative and positive lookaheads.

If you find yourself having to write very complex regex to parse XML, then you should seriously consider using an XML parser instead, which is a better tool for this.

Demo at:

2016-07-08

相关问答

例子 给定字符串foobarbarfoo : bar(?=bar) finds the 1st bar ("bar" which has "bar" after it)

bar(?!bar) finds the 2nd bar ("bar" which does not have "bar" after it)

(?<=foo)bar finds the 1st bar ("bar" which has "foo" before it)

(?

...

^(?=.{8}$).+ 将匹配字符串 aaaaaaaa 推理: 括号内的内容是向前看的,因为它以?=开头。 预览中的内容被解析 - 它不是从字面上解释的。 因此,如果.{8}$匹配,则前瞻只允许正则表达式匹配(在这种情况下,在字符串的开始处)。 所以字符串必须是正好八个字符然后它必须结束,如$证明。 然后,+会匹配这八个字符。 ^(?=.{8}$).+ will match the string aaaaaaaa Reasoning: The content inside of the brac

...

是否有任何理由不能使用与名称属性data-utime匹配的正则表达式,例如 data-utime="(.*?)"

这对于改变标签内相对位置的属性是稳健的,您不必处理负向和正向前瞻。 如果您发现自己必须编写非常复杂的正则表达式来解析XML,那么您应该认真考虑使用XML解析器,这是一个更好的工具。 演示: Regex101 Is there any reason you can't use a regex which matches the attribute data-utime by

...

你正在处理一个正则表达式,它必须意识到三个结局: ed\b ing\b ied\b 你必须考虑每个单点的存在。 例如, e[^d]\b和[^e]d\b 。 写下所有这些,你会得到这个正则表达式: ^(MAINT|(STRY|PRB)-\d+):\s*(?i:\w*(e[a-ce-z]|[a-df-z]d|i(n[a-fh-z]|[a-mo-z]g|e[a-ce-z]|[a-df-z]d)|[a-hj-z]ng|[a-hj-z][a-df-mo-z][a-cefh-z])|\w)\s([a-zA-Z

...

这是您尝试的另一种选择: (.+?)(?=\d{2}-\d{2}\\r\\n\d{2}-\d{2}|$)

Rubular Here's another option for you to try: (.+?)(?=\d{2}-\d{2}\\r\\n\d{2}-\d{2}|$)

Rubular

Lookaround实际上会匹配字符,但是会放弃匹配,只返回结果:匹配或不匹配。 你可以在这里找到更多信息。 Lookaround actually matches characters, but then gives up the match, returning only the result: match or no match. You can find more information here.

此问题的搭便车消息是:环视匹配位置 ,而不是字符串。 (?!e06772ed-7575-4cd4-8cc6-e99bb49498c5)

将匹配任何位置,而不是 e06772ed-7575-4cd4-8cc6-e99bb49498c5 。 意思就是: ^\/admin\/(?!(e06772ed-7575-4cd4-8cc6-e99bb49498c5)).*$

将匹配: /admin/abc

乃至: /admin/e99bb49498c5

但不是: /admin/e06772ed-7575-

...

你可以尝试: https:\/\/www.example.com\/.+?(?

这匹配: https://www.example.com/shop

但不是: https://www.example.com/de_de

Pythex链接在这里 说明:在这里我们使用一个负面后面(?

...

您的问题是您的捕获与模式中的单个+匹配,然后无法重复(即,输入中重复+会使匹配失败)。 试试这个正则表达式: ^>>(?:([ab])\+(?!.*\1))*c$

Your problem is that your capture matches the single + in the pattern which then cannot be repeated (that is, the repetition of + in your input makes the match fail). T

...

当某些元字符放在括号内时,不需要逃避。 换句话说,我不知道你是否意味着逃避*与\* 。 在这种情况下,尝试下一个: String newStr = str.replace("*", "\\*");

编辑 :你的正则表达式中有一些奇怪的东西。 (?=\[*])预测角色[ (0次或更多次),然后是] (?=[]\[*])展望下一个字符之一: [ , ] , * 也许您正在寻找的正则表达式如下: (?=\*)

在Java中, "(?=\\*)" When some metacharacters ar

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值