0

I have the following String

52x10x20x30x40

The string can be extended but with the same pattern and there will be other strings on both sides of it: for example

"Hello something 52x10x20x30x40 bla bla bla"

i want to capture all 2-digits.

I have the following regex

Pattern.compile("(\\d\\d)([x]\\d\\d)+");

But with this regex i only get the following groups:

1: 52
2: x40
0

3 Answers 3

5

Why not simply:

"52x10x20x30x40".split("x");

?

Forgot to mention that there can be other strings on both sides.

You could search for "\\d{2}(x\\d{2})+", and use split("x") on the match.

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

2 Comments

Forgot to mention that there can be other strings on both sides
@Jonas: You could search for "\\d{2}(x\\d{2})+", and use split() on the match.
0

Regex doesn't support variable group lengths.

Use a split method instead, e.g. Guava's Splitter:

Iterable<String> tokens = Splitter.on('x').split(str);

2 Comments

Forgot to mention that there can be other strings on both sides
@Jonas well then you'll have to get rid of those first. Perhaps you'll first search for the pattern (\\d\\d)(([x]\\d\\d)+) and then split matcher.group()
0

If you just want to capture all two digit numbers you could use this expression:

(?<!\d)(\d\d)(?!\d)

Usually you can only get the last substring that a repeated capturing group matches. (.NET regex differs in this regard.)

Comments

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.