2

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

1
  • There are quite a few significant differences between Python and Java, but your regex doesn't manage to stumble on any of them. Commented Oct 29, 2010 at 1:00

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I don't need to match the whole string, I just test if the pattern is in the string, so I test only for the first pair separated by a comma and that is sufficient for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.