0

I have the following string:

!date +10 (yyyy-MM-dd'T'HH:mm:ssz)

this string could be also (notice the minus instead of the plus.:

!date -10 (yyyy-MM-dd'T'HH:mm:ssz)

I need a regex pattern that will extract the numeric digits after the + (or -). There could be more than one digit.

I also need a pattern to extract the contents of the brackets ();

I've had a play around on regex pal. but couldn't get a working pattern.

Cheers.

1
  • 1
    What you have tried ?What should be your output? Commented Oct 4, 2012 at 9:42

3 Answers 3

3

To pick out the number & bracket content, you could do:

String str = "date +10 (yyyy-MM-dd'T'HH:mm:ssz)";
Matcher m = Pattern.compile(".*[+|-](\\d+).*\\((.*)\\).*").matcher(str);
if (m.matches()) {
    System.out.println(m.group(1));
    System.out.println(m.group(2));
}
Sign up to request clarification or add additional context in comments.

Comments

0

This regex should give you a match with the digits after the +/- and the contents of the parentheses in the first and second capturing group, respectively:

"!date\\s[+-](\\d+)\\s\\(([^)]*)\\)"

Comments

0

The following regex leads to 2 capturing groups with the contents you want

"!date\\s[+-](\\d+)\\s\\((\\d{4}-\\d{2}-\\d{2}'T'\\d{2}:\\d{2}:\\d{2}z)\\)"

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.