🎴 Flashcard Mode
Programming Languages and Database Concepts
import java.util.regex.; class Regex2 { public static void main(String[] args) { Pattern p = Pattern.compile(args[0]); Matcher m = p.matcher(args[1]); boolean b = false; while(b = m.find()) { System.out.print(m.start() + m.group()); } } } And the command line: java Regex2 "\d" ab34ef
The regex \d* matches zero or more digits. On string 'ab34ef', it matches: position 0 (empty), position 1 (empty), position 2 (digits '34'), position 4 (empty), position 5 (empty). The code prints m.start() + m.group() for each match: '0' + '' + '1' + '' + '2' + '34' + '4' + '' + '5' + '' = '01234456'. The pattern matches twice at position 4 because after consuming '34', zero-width matches occur at positions 4 and 5.