~/ learn/ comp-308/ cards/ Regular expressions: Pattern, Matcher & split
1 of 6

Type the Pattern/Matcher parse of an event line

Type the Pattern/Matcher parse of an event line

Answer

Pattern p = Pattern.compile("Event=(\\w+),time=(\\d+)"); Matcher m = p.matcher(line); if (m.matches()) { String name = m.group(1); long time = Long.parseLong(m.group(2)); }

Two capturing groups: group(1) is the event name, group(2) the digits. matches() requires the whole line to fit the pattern; \\w and \\d are the source escaping of the regex \w and \d.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/4921f4ce-0cf6-45be-82ed-9433d4ccdf22/flashcard utf-8 LF