Searching strings using regular expressions

Here is my code.

String tmpStr = "This {11} is a {2} simple string {3}";

int tmpFromIndex = 0;

int tmpToIndex = 0;

while ((tmpToIndex = tmpStr.indexOf("\\d{1, 2}", tmpFromIndex)) != -1)

{

String tmpCurrentTmpStr = tmpStr.substring(tmpFromIndex, tmpToIndex);

tmpFromIndex = tmpToIndex;

}

The tmpStr.indexOf("\\d{1, 2}") construction, which matches numbers in the given string always returns -1. How can I use it ?

[473 byte] By [mkArtaka] at [2007-11-27 11:32:33]
# 1

String's indexOf(...) method does not take a regex.

You need to have a look at the Pattern and Matcher classes and use Matchers' find() and group() methods.

prometheuzza at 2007-7-29 16:46:08 > top of Java-index,Java Essentials,New To Java...
# 2

Ok. Thank you also I have not specified the { and } symbols as special characters.

mkArtaka at 2007-7-29 16:46:08 > top of Java-index,Java Essentials,New To Java...
# 3

> Ok. Thank you also I have not specified the { and }

> symbols as special characters.

Ok, but do you still have a question?

prometheuzza at 2007-7-29 16:46:08 > top of Java-index,Java Essentials,New To Java...