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

Pages: 1-

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:15

String r = "\\d+D\\d+\\+\\d+";

Name: Anonymous 2009-02-24 16:15

Regular expressions
Java
No BBCode

Now you have problems.

Name: Anonymous 2009-02-24 16:16

Doesn't match search only at the beginning of the string? Try using search. Also set an ignore case flag when compiling.
__________________________
俺の若気をHAXX

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', '')]

Name: Anonymous 2009-02-24 16:19

>>5
Learn to r'(\d+)[dD](\d+)(\+\d+)?'!

Name: Anonymous 2009-02-24 17:39

IN SHORT, THIS IS MANDATORY, THE CODE TO ALIGN THE FINISHED YARN.

Name: Anonymous 2011-01-31 20:06

<-- check em dubz

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