abaplint / transpiler

ABAP to JS transpiler & runtime
https://transpiler.abaplint.org
MIT License
82 stars 23 forks source link

REGEX #672

Open mbtools opened 2 years ago

mbtools commented 2 years ago

ABAP regex is different from PCRE. However, it looks to me that the transpiler uses PCRE regex.

ABAP Example (from Phone Number exercise, which works without errors in ABAP but fails in Exercism):

    FIND REGEX '1*.*([2-9]\d\d).*([2-9]\d\d).*(\d\d\d\d).*(\d*)' IN `22234567890`
      SUBMATCHES DATA(p1) DATA(p2) DATA(p3) DATA(p4).

image

This works differently in JS

https://regex101.com/r/qhagS7/1

I can make the regex work in JS by using by making the * quantifiers lazy (replace by *?).

https://regex101.com/r/ZbSBTv/1

Not sure what's the best way to fix it. Here're the relevant docs:

https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenregex_migrating_posix.htm

larshp commented 2 years ago

can the example be reduced?

mbtools commented 2 years ago
FIND REGEX '(\d\d).*(\d*)' IN `123` SUBMATCHES DATA(p1) DATA(p2).

p1 = '12'. p2 = '3'.

https://regex101.com/r/SMEF5o/1 '12', null https://regex101.com/r/JAjp0V/1 '12', '3'

larshp commented 2 years ago

yea, try adding the lazy star thing, add some unit tests, we'll see if it works out 😄 but probably better than before

larshp commented 2 years ago

but note that https://github.com/abaplint/transpiler/pull/674 probably changes the same file that needs changes

larshp commented 2 years ago

new file for handling ABAP regex specifics, transpiler/packages/runtime/src/abap_regex.ts