Regular Expressions 101

Community Patterns

Regex for a line of assembly instruction

0

Regular Expression
Python

r"
^[ \t]*((?P<label>([a-zA-Z_][a-zA-Z0-9_]*))[ \t]*:)?[ \t]*((?P<opc>[a-zA-Z]+)[ \t]*([ \t](?P<op1>[a-zA-Z0-9_\.\"]+)([ \t]*\,[ \t]*(?P<op2>[a-zA-Z0-9_]+))?)?)?[ \t\n\f\r\v]*(;.*)?$
"
gm

Description

Returns 4 groups,

  • label = label used in the start, if any
  • opc = Opcode mnemonic if the line contains an instruction
  • op1 = first operand to mnemonic, if any
  • op2 = second operand to mnemonic, if any

If a value is absent, returns None. (Python does this)

Can be easily extended to 3 operand instructions by adding one more subgroup.

Line matching syntax

General syntax is -

LABEL:OPC OPERAND1,OPERAND2;Comment

where -
LABEL = (optional) 
OPC = (optional)
OPERAND1 = (optional, needs OPC)
OPERAND2 = (optional, needs both OPC and OPERAND1)
COMMENT = (optional)

Examples

  • this_is_a_label: MOV A, B ; Comments are also supported
  • ; Comments start with semicolon
  • empty_line_with_only_a_label:
  • MVI A, 01H ; No label in start
Submitted by anonymous - 5 years ago