PARSE NUMBERS FROM STRING

I have a string that I read that contains two numbers as strings for directories. eg. 0-63, a forward slash, and 0 -127 (64 rows, 128 cols). Is there a way to check the string for the possible patterns. I am trying

Pattern p = Pattern.compile("^0-77"+ "\\" + "0-177");

Matcher m = p.matcher(infile);

This does not work. It throws an Illegal octal escape sequence near index 7

^0-77\0-177

^

Would appreciate if anyone knows how to do this. the string I want to return is 0/0 thru 63/127.

[531 byte] By [seadazea] at [2007-11-27 4:27:30]
# 1
If it's "a forward slash" why the "\\" ?
xiarcela at 2007-7-12 9:36:09 > top of Java-index,Java Essentials,Java Programming...
# 2
it comes in as a backward slash from the file I am reading. I convert it to a forward slash later.
seadazea at 2007-7-12 9:36:09 > top of Java-index,Java Essentials,Java Programming...
# 3

In a java string, you have to escape a \ by writing \\. The String "\\" gets turned into "\" when compiled. When the regex reads that patter "\", it interprets it as an escape character. You have to escape the \ in the regex also. So use "\\\\" (4 backslashes) to end up with a regex of \\

Pattern p = Pattern.compile("^0-77\\\\0-177");

hunter9000a at 2007-7-12 9:36:09 > top of Java-index,Java Essentials,Java Programming...
# 4

Here's a regex for your problem.

Pattern p = Pattern.compile("^(([0-9])|([0-5][0-9])|(6[0-3]))/(([0-9])|([1-9][0-9])|(1[01][0-9])|(12[0-7]))$");

Matcher m = null;

String[] testCases = {"0/0", "5/10", "10/25", "59/107", "63/127", "64/133", "bogus/", "105/106"};

for( String s : testCases ) {

m = p.matcher( s );

System.out.println( s + " matches? " + m.matches() );

}

C_Walkera at 2007-7-12 9:36:09 > top of Java-index,Java Essentials,Java Programming...
# 5
using this string C:\\Data\\out\\19\\116\\cities202803.bin and this regex Pattern.compile("^(([0-9])|([0-5][0-9])|(6[0-3]))\\\\(([0-9])|([1-9][0-9])|(1[0-1][0-9])|(12[0-7]))$");it does not find the 19\116. Any idea why?
seadazea at 2007-7-12 9:36:09 > top of Java-index,Java Essentials,Java Programming...
# 6

It works for me. Did you use the String "19\\116"? Here's the exact code I tested:

Pattern p = Pattern.compile("^(([0-9])|([0-5][0-9])|(6[0-3]))\\\\(([0-9])|([1-9][0-9])|(1[0-1][0-9])|(12[0-7]))$");

Matcher m = null;

String[] testCases = {"0\\0", "5\\10", "10\\25", "59\\107", "63\\127", "64\\133", "bogus\\", "105\\106", "19\\116"};

for( String s : testCases ) {

m = p.matcher( s );

System.out.println( s + " matches? " + m.matches() );

}

Output:

0\0 matches? true

5\10 matches? true

10\25 matches? true

59\107 matches? true

63\127 matches? true

64\133 matches? false

bogus\ matches? false

105\106 matches? false

19\116 matches? true

hunter9000a at 2007-7-12 9:36:09 > top of Java-index,Java Essentials,Java Programming...
# 7

yes and it worked on that but not on my string......just trying to figure it out.

k = C:\\Data\\out\\19\\116\\cities202803.bin

Matcher mm = pp.matcher(infile);

if (mm.find())

{

start = mm.start();

end = mm.end();

}

String result = k.substring(start, end);

seadazea at 2007-7-12 9:36:09 > top of Java-index,Java Essentials,Java Programming...
# 8

The metacharacters '^' and '$' at the ends of the regex anchor it to the beginning and end of the target text. That's why the regex works with hunter's test data, but not with real, absolute file paths. Here's one way to adapt the regex: Pattern p = Pattern.compile("\\\\(\\d+)\\\\(\\d+)\\\\");

Matcher m = p.matcher(inFile);

if (m.find())

{

// it's good

}

I used (\\d+) as a placeholder; the important changes are the replacement of the anchors with backslashes, and the use of the find() method instead of matches().

uncle_alicea at 2007-7-12 9:36:09 > top of Java-index,Java Essentials,Java Programming...
# 9
Listen to uncle_alice, (he|she) is the (king|queen) of regex around here. ;p
hunter9000a at 2007-7-12 9:36:09 > top of Java-index,Java Essentials,Java Programming...
# 10
thanks!Tried something like that but just didn't have it right!
seadazea at 2007-7-12 9:36:09 > top of Java-index,Java Essentials,Java Programming...
# 11
Listen to uncle_alice, (he|she) is the (king|queen) of regex around here. ;p^^ ^^^^ ^_^
uncle_alicea at 2007-7-12 9:36:09 > top of Java-index,Java Essentials,Java Programming...