Reputation: 180
I've looked for a solution everywhere, I've tried with different regex flags, with no success.
I have the following regex pattern:
private static String LAST_LOG = "/.*?Last login:.*?td.*?td.*?([A-Za-z]+).*?;([0-9]+).*?;([0-9]+).*?;([0-9]+):([0-9]+):([0-9]+).*?([A-Z]+)/";
I'm trying to find a match in this string.
As you can see, it's finding a match, and capturing data in 7 groups (month, day, year, hour, minute, second, timezone).
The problem is that I'm not getting a match at all, I've tested the string and it does contain the part I'm looking for, so I can't find any other reason for this to not work in Android.
m = getMatcher(content,LAST_LOG);
if(m.find()) {
Log.e("Month",m.group(1));
Log.e("Day",m.group(2));
Log.e("Year",m.group(3));
Log.e("Hour",m.group(4));
Log.e("Minute",m.group(5));
Log.e("Second",m.group(6));
Log.e("Timezone",m.group(7));
}
private static Matcher getMatcher(String str, String regex){
Pattern pattern = Pattern.compile(regex);
return pattern.matcher(str);
}
And this is not the only regexp that is causing me trouble, I have others that are currently working, but simple modifications makes them fail, even thought on other platforms (and online regex testers) they work fine.
I have the same regexp in a Python script and it's working without any problems:
regex_last = r'.*?Last login:.*?td.*?td.*?([A-Za-z]+).*?;([0-9]+).*?;([0-9]+).*?;([0-9]+):([0-9]+):([0-9]+).*?([A-Z]+)'
m = re.search(regex_last,content.decode())
if m:
print(m.group(1))
print(m.group(2))
print(m.group(3))
print(m.group(4))
print(m.group(5))
print(m.group(6))
print(m.group(7))
else:
print('No match')
Outputs:
Apr
07
2016
17
33
52
CEST
I'm this close to switching to an HTML parser, but since I already had working regexps in both Python and Android and I figured using regular expressions would make it easier to switch between platforms, I decided to use them.
Upvotes: 1
Views: 476
Reputation: 6173
You need to remove the /
from the start and end. I would also remove the .*?
from the start as it serves no purpose but slows things down.
private static String LAST_LOG = "Last login:.*?td.*?td.*?([A-Za-z]+).*?;([0-9]+).*?;([0-9]+).*?;([0-9]+):([0-9]+):([0-9]+).*?([A-Z]+)";
Upvotes: 1