Parse integer from String of known format
I am reading from a file trying to read various settings by somewhat parsing lines of interest. A line of interest, here, is a line that begins with "var " followed by a variable name of interest (such as "mm" or "janYear"), followed by an equals sign (=), the value (an integer), a semicolon, and any amount of text afterwards.
In essense, it is a javascript file and I want to know the values of variables "mm" and "janYear". In C, this would be relatively simple using the sscanf() or fscanf function. Java, unfortunately, has no such function along those lines, so I'm trying to figure out a slightly less direct approach hopefully without having to break down and write my own character-by-character parser.
The two strings follow the following regular expression format:
mm = "var\s+mm\s*=\s*\d\s*;.*"
jarYear = "var\s+jarYear\s*=\s*\d*\s*;.*"
mm usually happens to be either 0 or 6, so I've used the workaround of checking if it matches the following format:
"var\s+mm\s*=\s*6\s*;.*"
then mm = 6, else mm = 0.
Unfortunately, this is not so easy with janYear, since it can be any number, and any number of digits (within reason).
The workaround I have come to so far is to use replaceFirst with the regular expression up to but excluding the \d*, so I can bring it down to the following format:
janYear = "\d*\s*;.*"
although I'd much rather just get it done using the original regular expression, rather than having to widdle it down and pluck my number.
For those who are need a quick refresher with regular expressions:
\s indicates a whitespace
\d indicates a decimal [0-9]
. indicates any character
* indicates that the preceding character is repeated 0 or more times
+ indicates that the preceding character is repeated 1 or more times
Thanks in advance
-IsmAvatar

