开发者

Differences in regex syntax in Python and Java

开发者 https://www.devze.com 2023-01-22 17:07 出处:网络
I have a following working regex in Python and I am trying to convert it to Java, I thought that regex works the same in both languages, but obviously it doesn\'t.

I have a following working regex in Python and I am trying to convert it to Java, I thought that regex works the same in both languages, but obviously it doesn't.

Python regex: ^\d开发者_如何转开发+;\d+-\d+

My Java attempt: ^\\d+;\\d+-\\d+

Example strings that should be matched:

3;1-2,2-3
68;12-15,1-16,66-1,1-2

What is the right solution in Java?

Thank you, Tomas


The regex is faulty for the input, don't know what you were doing in Python, but this isn't matching the whole strings in any regex I know.

This should do the trick (escaping characters are omitted):

^\d+;(\d+-\d+,?)+

I.e. you need to continue matching the number pairs separated by commas.

0

精彩评论

暂无评论...
验证码 换一张
取 消