Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

REGEX /java

Name: Anonymous 2009-02-24 16:11

Trying to create a dicerolling application that, among other input, accepts a 'roll expression' from a textfield(eg. 4D20+1). Regex then matches this up, and if correct, breaks the statement down, 'rolls' and returns a result. For the moment I'm jsut looking for true/false for match.

I'm having trouble getting regex to match though.
Can any of you fine chaps help?

String r = "\\d+d\\d+\\+\\d+";
try{
    pattern = Pattern.compile(r);
   }
catch(Exception e){
   return(e.getMessage());
   }

matcher = pattern.matcher(r);

Boolean match = (matcher.matches());
return(match.toString());

This always report false.
I know my regex statement is the problem, what with the '+' character(probably not the only source of fail...?)

Any help gents?

Name: Anonymous 2009-02-24 16:16

>>> r = re.compile("(\\d+)[dD](\\d+)(\\+\\d+)?")
>>> r.findall('4D20+1')
[('4', '20', '+1')]
>>> r.findall('1d6')
[('1', '6', '')]

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List