英文:
Regex to extract <*n (where n is a number)
问题
我有一个字符串的形式
"abcd<*15<<defgj<*3fsdfsdf"
我想提取所有的<或者<*后面跟着一个数字,比如<*15,<*8,<*1等等。
我有这个正则表达式:
<\*?[0-9+]?
在示例字符串中匹配到了:
<*1
<
<
<*3
但我希望它能匹配到第一个作为<*15。
我以为正则表达式中的+可以实现这个功能,所以我不确定为什么它不起作用。
我还尝试了
<\*?[\d+]?
但仍然只能匹配到<*后面最多一个数字。
一个<要么是独立的,要么后面跟着一个*和一个数字。我想把<6捕获为<。
如果<后面跟着一个*,但没有数字,比如<*abc,我只想捕获<。
英文:
I have a string of the form
"abcd<*15<<defgj<*3fsdfsdf"
I want to pull out all the < or <* followed by a number, e.g. <*15, <*8, <*1 etc
I have this:
<\*?[0-9+]?
Which in the example strings matches
<*1
<
<
<*3
But I'd like it to match the first one as <*15
I thought the + did this in the regex, so I'm not sure why it's not working.
I also tried
<\*?[\d+]?
But still I get at most one digit following <*
A < will always either be on it's own, or followed by a * followed by a number. I want to capture <6 as just <
If the < is followed by a *, but then no number. e.g. <*abc I would just want to capture <
答案1
得分: 2
你需要
<(?:\*\d+)?
请参见正则表达式演示。
详细信息:
<- 一个<字符(?:\*\d+)?- 一个可选的非捕获组,匹配一个或零个*字符,后面跟着一个或多个数字。
请参见Go演示:
package main
import (
"fmt"
"regexp"
)
func main() {
r := regexp.MustCompile(`<(?:\*\d+)?`)
matches := r.FindAllStringSubmatch("<*1 < <6", -1)
for _, v := range matches {
fmt.Println(v[0])
}
}
输出
<*1
<
<
英文:
You need
<(?:\*\d+)?
See the regex demo.
Details:
<- a<char(?:\*\d+)?- an optional non-capturing group matching one or zero repetition of a*char and one or more digits after it.
See the Go demo:
package main
import (
"fmt"
"regexp"
)
func main() {
r := regexp.MustCompile(`<(?:\*\d+)?`)
matches := r.FindAllStringSubmatch("<*1 < <6", -1)
for _, v := range matches {
fmt.Println(v[0])
}
}
Output
<*1
<
<
答案2
得分: 1
我会使用\<\*{1}\d+|\<。
解释:
\<匹配小于号字符\*{1}后面跟着一个星号\d+后面跟着任意数量的连续数字|\<或者只匹配一个<,如果前面的条件不匹配
import (
"fmt"
"regexp"
)
func main() {
pattern := regexp.MustCompile(`\<\*{1}\d+|\<`)
inputString_1 := "abc<*15<<def<*3ghi"
inputString_2 := "jkl<42<<mno<7pqrst"
fmt.Println(pattern.FindAllString(inputString_1, -1))
fmt.Println(pattern.FindAllString(inputString_2, -1))
}
// 输出:
// [<*15 < < <*3]
// [< < < <]
Playground: https://go.dev/play/p/xNmsrilYGRL
正则表达式: https://regex101.com/r/6TtLtl/1
关于你的备注...
> 我以为在正则表达式<\*?[0-9+]?中,+会起作用,所以我不确定为什么它不起作用。
[0-9+]? 贪婪地匹配任何数字 或 一个加号 或 一个空字符串。
参考: https://www.rexegg.com/regex-quantifiers.html
英文:
I would use \<\*{1}\d+|\<
explanation:
\<Match the lesser than char\*{1}Followed by an asterix\d+Followed by any number of sequential digits|\<Or just match a<if the above does not match
import (
"fmt"
"regexp"
)
func main() {
pattern := regexp.MustCompile(`\<\*{1}\d+|\<`)
inputString_1 := "abc<*15<<def<*3ghi"
inputString_2 := "jkl<42<<mno<7pqrst"
fmt.Println(pattern.FindAllString(inputString_1, -1))
fmt.Println(pattern.FindAllString(inputString_2, -1))
}
// Output:
// [<*15 < < <*3]
// [< < < <]
Playground: https://go.dev/play/p/xNmsrilYGRL
Regex: https://regex101.com/r/6TtLtl/1
Regarding your note...
> I thought the + did this in the regex <\*?[0-9+]? so I'm not sure why it's not
> working.
[0-9+]? greedily matches any digit or a "+" or an empty string.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论