java - Get last n matches from String with regex -
i have string html withoutany kind of close tag (</.*?>
) , without new line (\n
):
<tr><td align=center>01/01/2001<td align=center>500,01<td align=center>0,99<td align=center>15
this repeat indefinitely , may have 1 or more td's values. @ moment using string.split("<tr><td align=center>")
separate string , use 1 regex find date , 1 find value want.
something this:
string[] stringarray = text.split("<tr><td align=center>"); string[] array1 = arrays.copyofrange(stringarray, stringarray.length - /*0<n<21*/, stringarray.length); (int = 0; < array1.length; i++) { system.out.println(array1[i]); m1 = pattern.compile("(\\d{2}\\/\\d{2}\\/\\d{4})").matcher( array1[i]); //getting date m1.find(); system.out.println(m1.group(1)); m1 = pattern.compile("<td align=center>(\\d+,*\\d*)").matcher(array1[i]); while (m1.find()) { system.out.println(m1.group(/*0<n*/)); } }
i want way string equivalent array1 (the last n positions of string) using regex.
i know can use bigger regex $
@ end last <tr>
, want 19 <tr>
before to.
i don't know if being clear here. let me know if can provide more details.
ps: yes values writen ',' instead of '.'... use replace later on.
with java regular expressions can't collect arbitrary number of matches single group, unless know exact/maximum number of groups you'd have apply regex multiple times , collect matches yourself.
btw, should check whether m1.find();
returns true before calling m1.group(1);
otherwise you'd illegalstateexception if expression doesn't match.
as note, i'd compile date pattern outside loop, in initialization code.
Comments
Post a Comment