-1

I'm attempting to create a regex that allows identification of strings that are operands in assembly code. In order to do this I need to identify various addressing mode styles within the assembly code.

I have this regex: ([$\(\),#])\w+/g which correctly identifies all of the operands in the following text, however regexr.com is telling me that the regex doesn't identify the parentheses in the line containing the LDA instruction.

How do I need to modify this regex so that it also includes the parentheses?

        begin
        LDA #10         ;load $0A into accumulator
        LDX $15         ;load $15m into X
_loop   TAX             ;transfer A to X 
        SEC             ;set carry flag
_branch ADC $10         ;add with carry instruction on absolute memory address 0x0A
        STA $3000,X
        LDA ($40),Y    
        end
  • Maybe https://regex101.com can help to figure it out. – Heinz Schilling Feb 12 '18 at 20:22
  • Not completely sure what you're trying to match vs. capture, but: `\(?([\$#,])\w+\)?` You have to escape the `$` unless you're intending for it mean the beginning of the line. Which...I'm not sure you aren't. – zzxyz Feb 12 '18 at 20:26
  • 1
    @zzxyz In most regex flavors, if the `$` is within a character class, as this one is, it will be interpreted literally. – Ron Rosenfeld Feb 12 '18 at 20:28
  • Try with the parentheses outside of the character class, and optional. eg: `(\(?[$,#])\w+\)?` – Ron Rosenfeld Feb 12 '18 at 20:34
  • @RonRosenfeld - Thanks for the correction. I know that, and always forget it. – zzxyz Feb 12 '18 at 20:50

1 Answers1

-1

Your regexp only matches a word with one punctuation character before it, and doesn't match punctuation after it. You need to add a quantifier after the brackets, and another set of brackets after the word.

/([$\(\),#])+\w+[$\(\),#])/g
Barmar
  • 596,455
  • 48
  • 393
  • 495