S
stevengarcia
I'm having trouble writing a regular expression (using Java) to extract
out the numeric portion of a String. Some examples are
"$.50." --> .50
"$0.50." --> 0.50
"$6.25." --> 6.25
"$12.30." --> 12.30
"$6.25" --> 6.25
"$0.50" --> 0.50
"2350." --> 2350
"10." --> 10
"$23a.4." no match
Note that the first character can optionally be a $, and the last
character can optionally be a .
Thus far I have a regular expression that is
^\\$?(\\d*\\.?\\d*)\\.?
which reads "0 or 1 dollar sign, 0 or more digits, 0 or 1 period, 0 or
more digits, 0 or 1 period."
As far as I can tell this is the correct expression. But I want to
extract out what is in-between the parenthesis, as shown in the
expression. It's not working and my simple program is below (with
results):
I would appreciate any help!
public static void main(String[] args) {
String[] lines = new String[] { "$.50.",
"$0.50.",
"$6.25.",
"$12.30.",
"$6.25",
"$0.50",
"2350.",
"10.",
"$23a.4."};
String regexp = "^\\$?(\\d*\\.?\\d*)\\.?";
Pattern pattern = Pattern.compile(regexp);
for (int i = 0; i < lines.length; i++) {
System.out.println("Does text " + lines + " match
regexp? " + lines.matches(regexp));
if (lines.matches(regexp)) {
Matcher matcher = pattern.matcher(lines);
while (matcher.find()) {
System.out.println(" " + lines + " matches " +
regexp + " with " + matcher.group() +
" Start <" + matcher.start() +
">, End <" + matcher.end() + ">.");
}
}
}
}
and the output I get is
Does text $.50. match regexp? true
$.50. matches ^\$?(\d*\.?\d*)\.? with $.50. Start <0>, End <5>.
Does text $0.50. match regexp? true
$0.50. matches ^\$?(\d*\.?\d*)\.? with $0.50. Start <0>, End <6>.
Does text $6.25. match regexp? true
$6.25. matches ^\$?(\d*\.?\d*)\.? with $6.25. Start <0>, End <6>.
Does text $12.30. match regexp? true
$12.30. matches ^\$?(\d*\.?\d*)\.? with $12.30. Start <0>, End <7>.
Does text $6.25 match regexp? true
$6.25 matches ^\$?(\d*\.?\d*)\.? with $6.25 Start <0>, End <5>.
Does text $0.50 match regexp? true
$0.50 matches ^\$?(\d*\.?\d*)\.? with $0.50 Start <0>, End <5>.
Does text 2350. match regexp? true
2350. matches ^\$?(\d*\.?\d*)\.? with 2350. Start <0>, End <5>.
Does text 10. match regexp? true
10. matches ^\$?(\d*\.?\d*)\.? with 10. Start <0>, End <3>.
Does text $23a.4. match regexp? false
out the numeric portion of a String. Some examples are
"$.50." --> .50
"$0.50." --> 0.50
"$6.25." --> 6.25
"$12.30." --> 12.30
"$6.25" --> 6.25
"$0.50" --> 0.50
"2350." --> 2350
"10." --> 10
"$23a.4." no match
Note that the first character can optionally be a $, and the last
character can optionally be a .
Thus far I have a regular expression that is
^\\$?(\\d*\\.?\\d*)\\.?
which reads "0 or 1 dollar sign, 0 or more digits, 0 or 1 period, 0 or
more digits, 0 or 1 period."
As far as I can tell this is the correct expression. But I want to
extract out what is in-between the parenthesis, as shown in the
expression. It's not working and my simple program is below (with
results):
I would appreciate any help!
public static void main(String[] args) {
String[] lines = new String[] { "$.50.",
"$0.50.",
"$6.25.",
"$12.30.",
"$6.25",
"$0.50",
"2350.",
"10.",
"$23a.4."};
String regexp = "^\\$?(\\d*\\.?\\d*)\\.?";
Pattern pattern = Pattern.compile(regexp);
for (int i = 0; i < lines.length; i++) {
System.out.println("Does text " + lines + " match
regexp? " + lines.matches(regexp));
if (lines.matches(regexp)) {
Matcher matcher = pattern.matcher(lines);
while (matcher.find()) {
System.out.println(" " + lines + " matches " +
regexp + " with " + matcher.group() +
" Start <" + matcher.start() +
">, End <" + matcher.end() + ">.");
}
}
}
}
and the output I get is
Does text $.50. match regexp? true
$.50. matches ^\$?(\d*\.?\d*)\.? with $.50. Start <0>, End <5>.
Does text $0.50. match regexp? true
$0.50. matches ^\$?(\d*\.?\d*)\.? with $0.50. Start <0>, End <6>.
Does text $6.25. match regexp? true
$6.25. matches ^\$?(\d*\.?\d*)\.? with $6.25. Start <0>, End <6>.
Does text $12.30. match regexp? true
$12.30. matches ^\$?(\d*\.?\d*)\.? with $12.30. Start <0>, End <7>.
Does text $6.25 match regexp? true
$6.25 matches ^\$?(\d*\.?\d*)\.? with $6.25 Start <0>, End <5>.
Does text $0.50 match regexp? true
$0.50 matches ^\$?(\d*\.?\d*)\.? with $0.50 Start <0>, End <5>.
Does text 2350. match regexp? true
2350. matches ^\$?(\d*\.?\d*)\.? with 2350. Start <0>, End <5>.
Does text 10. match regexp? true
10. matches ^\$?(\d*\.?\d*)\.? with 10. Start <0>, End <3>.
Does text $23a.4. match regexp? false