这是我写过的最长的正则表达式,没有之一
提取日志信息的正则表达式
import java.util.regex.Matcher; import java.util.regex.Pattern; class Untitled { public static void main(String[] args) { Pattern pattern = Pattern.compile( "(?<date>\\S*)\\s(?<time>\\S*)\\s\\[(?<Grade>\\S*)\\]\\s(?<ProcInfo>\\S*)\\:\\s\\*(?<ProcInfoDetail>\\S*)\\s\\[lua\\]\\s(?<ScriptFile>\\S*)\\:(?<RowNum>\\d*)\\s(?<MethodName>\\S*)\\(\\)\\:\\s\\[(?<GatewayVer>\\S*)\\]\\s\\[(?<GateStatus>\\d*)\\]\\,\\s\\[(?<ApiId>\\d*)\\]\\,\\s\\[(?<ApiStatus>\\d*)\\,\\s(?<ResponseTime>\\S*)\\,\\s(?<InnerRespTime>\\S*)\\,\\s(?<InnerStrartReqTime>\\S*)\\,\\s(?<BodySize>\\d*)\\]\\,\\s\\[(?<ReqTime>\\S*)\\,\\s(?<ClientIp>\\S*)\\,\\s(?<UID>\\S*)\\,\\s(?<RequestSize>\\d*)\\]\\,\\s\\[(?<RequestId>\\S*)\\,\\s(?<TraceId>\\S*)\\,\\s(?<ServiceId>\\S*)\\]\\,\\s\\[(?<IsLocalIp>\\S*)\\,\\s(?<KeySecCheckRes>\\S*)\\,\\s(?<SessionChkRes>\\S*)\\,\\s(?<SID>\\S*)\\,\\s(?<UcenterPlat>\\S*)\\,\\s(?<CheckLogTokenRes>\\S*)\\,\\s(?<Cookie>[^\\,]*)\\,\\s(?<InvalidMsg>\\S*)\\]\\,\\s\\[(?<SessionVarMap>\\S*)\\]\\,\\s\\[(?<RealIp>\\S*)\\,\\s(?<UserAgent>[^\\,]*)\\,\\s(?<LastHop>\\S*)\\,\\s(?<XF>[^\\]]*)\\]\\,\\s\\[(?<RequestBody>[^\\]]*)\\]\\,\\s\\[(?<RespBody>[^\\]]*)\\][\\s\\S]*?client\\:\\s(?<client>[^\\,]*)[\\S\\s]*?request\\:\\s(?<request>[^\\,]*)[\\S\\s]*?host\\:\\s(?<host>[^\\,]*)[\\S\\s]*?referrer\\:\\s(?<referrer>[^\\,]*)[\\S\\s]*"); String message="2016/06/15 10:35:02 [notice] 16639#0: *7736383035 [lua] gateway.lua:155: log(): [GatewayMonV2] [200], [2050], [200, 0.10800004005432, 0.10800004005432, 1465958102.646, 116], [1465958102.646, 60.167.21.130, -, 469], [-, 10.209.240.18-1465958102.646-16639-1595, -], [false, -, -, -, -, -, SESSIONID=deleted, -], [{}], [-, Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1), 60.167.21.130, table: 0x41f0b290], [FFClientType=2&userNameType=2&verifyCode=837664&FFClientVersion=25200&imei=8b1e5f52b02c66c6e63304527729bb92o775Ptx4&appid=feifan&userName=13094392437&devInfo=%7B%22IMEI%22%3A%22460001159143398%22%2C%22IMSI%22%3A%22460070050561920%22%2C%22cell_id%22%3A%22%7B%5C%22cells%5C%22%3A%5B%7B%5C%22mnc%5C%22%3A7%2C%5C%22mcc%5C%22%3A460%2C%5C%22cid%5C%22%3A28883%2C%5C%22strength%5C%22%3A0%2C%5C%22lac%5C%22%3A4527%7D%5D%7D%22%2C%22device_desc%22%3A%22HTC+T328w%22%2C%22device_id%22%3A%22460001159143398%22%2C%22ip%22%3A%2210.0.2.15%22%2C%22network_desc%22%3A%22Wifi%22%2C%22os_type%22%3A%22android%22%2C%22os_version%22%3A%22Android2.2.3%22%2C%22phone_wifi_mac%22%3A%2200%3A50%3A56%3AC0%3A00%3A08%22%2C%22router_mac%22%3A%2200%3A50%3A56%3AC0%3A00%3A08%22%7D&ddId=8b1e5f52b02c66c6e63304527729bb92o775Ptx4&password=****&channel=18&wdId=8b1e5f52b02c66c6e63304527729bb92&], [{\"status\":200,\"message\":\"\u6210\u529f\",\"data\":{\"puid\":\"CA16C694353A462DA817C77F5EE82797\",\"uid\":\"15000000074366911\"}}], [-, -, -, -, -, -, -, -, -, -, -], [-End-] while sending to client, client: 60.167.21.130, server: localhost, request: \"POST /ffan/v1/member/register HTTP/1.1\", host: \"api.ffan.com\", referrer: \"http://api.ffan.com/ffan/v1/member/register\""; Matcher matcher = pattern.matcher(message); if (matcher.find()) { String RespBody=matcher.group("RespBody"); System.out.println(RespBody); } } }