Natural Language Processing · TEE · papers

Q1d · Five regex operators

2024-02-12 · re-exam · recovered AI paper

Previous examination · complete worked answers

Question. For word-pattern detection, explain: ^abc, abc$, [A-Z0-9], ed|ing|s, [abc]. [5 marks]

Answer

PatternExact meaningPositive exampleImportant distinction
^abcabc at start of the input/line under chosen modeabcdefNot abc anywhere
abc$abc at endxxabcNot starts with abc
[A-Z0-9]One uppercase ASCII letter or digitQ or 7One character, not the whole string
ed|ing|sOne of the alternatives ed, ing or smatched edWithout anchors this is not necessarily a suffix
[abc]One a, b or cbNot the three-character string abc

Use full-string matching when the entire string must satisfy a pattern; a search may find only a substring.