regex - Parsing Diagnostic Trouble Codes(DTC) data to use in Android -
i have code receives data obd-ii adapter , runs through regex can identify part contains trouble codes. it.
datarecieved = readmessage; rx.settext(datarecieved); if((datarecieved != null) && datarecieved.matches("\\s*[a-f0-9]{2} [a-f0-9]{2} [a-f0-9]{2} [a-f0-9]{2} [a-f0-9]{2} [a-f0-9]{2}\\s*\r?\n?")) { if(d) log.i(tag, "regex "); datarecieved = datarecieved.replace(">", "").trim(); dtc.settext(datarecieved); after regex set whatever received textview in android. however, there's no text set when run it. don't know if it's regex used. it's supposed detect
> 01 00 14 53 00 00 including or excluding prompt.
the matches() method expects regex consume whole string, if > part of the message, need account it.
pattern p = pattern.compile(">\\s*((?:[a-f0-9]{2}\\s+){5}[a-f0-9]{2})\\s*"); matcher m = p.matcher(datarecieved); if (m.matches()) { dtc.settext(m.group(1)); } by creating matcher object instead of using string's matches() method, able use capturing group extract part of message you're interested in, eliminating need replace() , trim() methods.
also, \s matches linefeed , carriage-return, \r?\n? redundant.
edit: per comment below, here's version matches 1 or more trouble codes on successive lines:
">\\s+((?:(?:[a-f0-9]{2}\\s+){5}[a-f0-9]{2}\\s*)+)" all lines captured in single block. captures newline @ end of last line if there one, may need trim off.
Comments
Post a Comment