正则表达式捕获组的变化,0次或更多次。

huangapple go评论61阅读模式
英文:

Regex that captures variations of a capture group 0 or more times

问题

I understand that you're looking for help with a regex pattern to capture variations of (type|data|reason) in a string. However, I won't provide the full solution here as requested. Here's a modified part of your provided pattern that captures the values:

(?:type|data|reason)=([0-9a-z:.%\-]+)(?:&|$)

You can use this pattern as a starting point and adapt it to your specific needs in Android Java/Kotlin.

英文:

Trying to understand how to come up with a regex that allows a capture group containing variations to match all of its variations found in a string.

Example of the string as follows:

/sometexthere/moretexthere?type=1234&data=56357782&noextract=6532

Goal is to return 1234 and 56357782 but not 6532 using a pattern similar to the following, which is how far I was able to get:

/sometexthere/moretexthere\?(?:&?(?:type|data|reason)=([0-9a-z:.%\-]+)*)+

Another example string would be

/sometexthere/moretexthere?data=56357782&noextract=6532&type=1234

And would return 56357782 and 1234 but not 6532

And another example

/sometexthere/moretexthere?reason=1234

And would return 1234

The goal is to return the value that is part of the (type|data|reason) variations, so whenever there is a type, data, or reason - regardless of their order - in the string it will return the value that is in front of each one of them, not just the first/last match it finds.

Unfortunately the best regex I could come up with, exemplified above, is only returning the single last match of the string and not including any other matches before.

Don't know if it can have any relevance, but this is meant to be used in Android Java/Kotlin.

答案1

得分: 2

If you know in advance that your string will be located after one of the three keywords "type", "data" or "reason", you can directly use them with lookarounds to catch your required numbers.

(?<=(?:/sometexthere/moretexthere\?|&amp;)\b(?:type|data|reason)\b=)([0-9a-z:.%\-]+)

Regex Explanation:

  • (?<=(?:/sometexthere/moretexthere\?|&amp;)\b(?:type|data|reason)\b=): lookaround
    • (?:/sometexthere/moretexthere\?|&amp;): non-capturing group with your path or "&"
    • \b: word boundary
    • (?:type|data|reason): non-capturing group with one of the three words
    • \b: word boundary
    • =: "equals" sign
  • ([0-9a-z:.%\-]+): your match

Check the demo here.

英文:

If you know in advance that your string will be located after one of the three keywords "type", "data" or "reason", you can directly use them with lookarounds to catch your required numbers.

(?&lt;=(?:/sometexthere/moretexthere\?|&amp;)\b(?:type|data|reason)\b=)([0-9a-z:.%\-]+)

Regex Explanation:

  • (?&lt;=(?:/sometexthere/moretexthere\?|&amp;)\b(?:type|data|reason)\b=): lookaround
    • (?:/sometexthere/moretexthere\?|&amp;): non-capturing group with your path or "&"
    • \b: word boundary
    • (?:type|data|reason): non-capturing group with one of the three words
    • \b: word boundary
    • =: "equals" sign
  • ([0-9a-z:.%\-]+): your match

Check the demo here.

huangapple
  • 本文由 发表于 2023年5月18日 01:49:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定