OuhscBbmc / StatisticalComputing

OUHSC's SCUG (Statistical Computing Users Group)
MIT License
8 stars 3 forks source link

Another regular expression for Monday #9

Closed Maleeha closed 4 years ago

Maleeha commented 4 years ago

I want to put comma in currency:

22300

The following regular expression with positive look ahead and negative look ahead, was working on regex 101

(\d)(?=(\d{3})+(?!\d))

You replace it with:

\1,

However its not working for me on R. Any suggestions? @wibeasley

Thanks!

wibeasley commented 4 years ago

Escape the escape.

In other words, use two backslashes: (\\d)(?=(\\d{3})+(?!\\d)) and \\1.

But those lookarounds are tricky. I think there are some of themm that aren't fully supported in R. Maybe negative look behinds?

Maleeha commented 4 years ago

Thank you. I did escape what i needed to escape but as you said it was perhaps not supported by R. I will look into negative look behind.

wibeasley commented 1 year ago

I saw this as we were just talking about regexes & SCUG issues. If anyone is coming back to it...

make sure perl = TRUE is specified as an argument to sub() or gsub(). I think it's necessary for look-arounds.