Nick
Nick

Reputation: 729

Regex pattern issue

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.

Upvotes: 0

Views: 85

Answers (3)

Reimeus
Reimeus

Reputation: 159754

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));
}

Upvotes: 3

halex
halex

Reputation: 16403

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)\\)"

Upvotes: 0

skunkfrukt
skunkfrukt

Reputation: 1570

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\\(([^)]*)\\)"

Upvotes: 0

Related Questions