There is a long string of which the structure is <A>N</C></B> <B >E</B> <B >N</B> and repeats many times.
(A,B,C,E,N each represents different string)
eg:
<target="FmRight">200910102</A></TD> <TD Nowrap >alvin</TD> <TD Nowrap >93</TD> <target="FmRight">200910103</A></TD> <TD Nowrap >Tom</TD> <TD Nowrap >85</TD>
I want to retrieve the string between the tag , so I wrote two regular expressions
"target=\"FmRight\">\\d+"
"<TD Nowrap >[^<]*</TD>"
and here is the test code
Pattern p1 = Pattern.compile("target=\"FmRight\">\\d+");
Pattern p2 = Pattern.compile("<TD Nowrap >[^<]*</TD>");
Matcher m = p1.matcher(text);
int count = 0;
for (int i = 0; i < 3; i++) {
if(m.find())
System.out.println(m.group().split(">")[1]);
m.usePattern(p2);
count=0;
for(int j=0;j<2;j++){
if(m.find())
System.out.println(m.group().split(">")[1].split("<")[0]);
}
m.usePattern(p1);
count=1;
}
In jre,it will run properly,but in android, it won’t .
Because in java usePattern()
This method causes this matcher to lose information about the groups
of the last match that occurred. The matcher’s position in the input
is maintained and its last append position is unaffected.
in android usePattern()
Sets a new pattern for the Matcher. Results of a previous find get lost. The next attempt >to find an occurrence of the Pattern in the string will start at the beginning of the >input.
so,how can I maintain the input position while changing the Pattern in android??
You can use the
region()method to force the Matcher to start looking from the position where the lastfind()ended.EDIT: Correction: you should be able to do this. I don’t speak Android, so I don’t know if they’ve messed this up, too. :-/