java - Regular expression to fetch username= 'testuserMM' from the string -


i tried regex capture username

highs\(\d+\)\[.*?\]\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]\sftid\(\d+\):\s. 

it didn't work.

<55>mar 17 12:02:00 forcesss-off [father][1x91422234][eee][hote] abcd(qlidcxpoulqsf): highs(23455814)[mothers][192.192.21.12] ftid(64322816): oops authentication failed (http-commo-auth, username='testusermm' password='********'congratulation-fakem='login' ) 

you can use simpler regex that:

\busername='([^']+) 

see demo, result in group 1.

regex:

  • \b - word boundary
  • username=' - literal string username='
  • ([^']+) - capturing group containing our substring contains 1 or more symbols other single apostrophe.

update:

here 2 ways text looking for:

string str = "<55>mar 17 12:02:00 forcesss-off [father][1x91422234][eee][hote] abcd(qlidcxpoulqsf): highs(23455814)[mothers][192.192.21.12] ftid(64322816): oops authentication failed (http-commo-auth, username='testusermm' password='********'congratulation-fakem='login' )"; string res = str.replaceall(".*\\busername='([^']+)'.*", "$1"); system.out.println(res);  string rx = "(?<=\\busername=')[^']+"; pattern ptrn = pattern.compile(rx); matcher m = ptrn.matcher(str); while (m.find()) {    system.out.println(m.group()); } 

see ideone demo


Comments