英文:
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\?|&)\b(?:type|data|reason)\b=)([0-9a-z:.%\-]+)
Regex Explanation:
(?<=(?:/sometexthere/moretexthere\?|&)\b(?:type|data|reason)\b=)
: lookaround(?:/sometexthere/moretexthere\?|&)
: 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.
(?<=(?:/sometexthere/moretexthere\?|&)\b(?:type|data|reason)\b=)([0-9a-z:.%\-]+)
Regex Explanation:
(?<=(?:/sometexthere/moretexthere\?|&)\b(?:type|data|reason)\b=)
: lookaround(?:/sometexthere/moretexthere\?|&)
: 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论