java - matcher.group(1) return no result -
public class getprop extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); string file_proc = readfile(); textview tv = (textview)findviewbyid(r.id.tv); tv.settext("read file contents sdcard : \n" + file_proc); } public string readfile(){ bufferedreader rdr; string proc = ""; string line; int linenumber = 0; try { rdr = new bufferedreader(new filereader("/proc/cpuinfo")); while ((line = rdr.readline()) != null) { linenumber++; matcher matcher = pattern.compile("processor: (.*)").matcher(line); if (matcher.find()) { proc = matcher.group(1); } } } catch (exception e) { e.printstacktrace(); } return proc; } }
i want print 1 line /proc/cpuinfo txt file, processor: "result", result matcher.group(1). have no text in th result, problem?
this /proc/cpuinfo
looks like:
processor : 0 vendor_id : genuineintel cpu family : 6 model : 42 /* etc */
there 2 problems regex:
- regular expressions case sensitive. either change
processor
, or usepattern.compile(..., pattern.case_insensitive)
. - there whitespace between
processor
, colon. should change regexprocessor\\s*: (.*)
Comments
Post a Comment