Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regular Expression repalace with Uppercase
#1
I've seen documentation for regular express where syntax like this \U$1 would replace the match with an upper case
However in QM, _s.replacerx("(?i)([AP])\.M\." "\U$1M") outputs "\UpM"

Do I have it wrong or is this syntax not implemented in QM?

-Jim
#2
QM does not support \U.
Need a callback function:
menu File -> New -> Templates -> Callback -> Callback_str_replacerx.

Macro Macro2433
Code:
Copy      Help
str s=
;Tuesday-Friday 9:00 a.m. to 9:00 p.m.
;Tuesday-Friday 9:00 am to 9:00 pm
;Tuesday-Friday 9:00 AM to 9:00 PM
REPLACERX ff.frepl=&sub.Callback_str_replacerx
s.replacerx("\b(am|pm|a\.m|p\.m)\b" ff 1)
out s


#sub Callback_str_replacerx
function# REPLACERXCB&x

;Callback function for str.replacerx.
;Called each time when a match is found during replacement process. Provides replacement string.
;x.match initially contains the matched substring. This function can modify it. It will become replacement string.
;More info in QM Help.

;Return:
;0 - match is replacement string. It will be appended to string being formatted. If match is not modified, matched substring will not be replaced.
;> 0 - nothing should be appended to string being formatted. This return value can be used either to remove matched substring, or when callback function itself appends replacement to strnew.
;-1 - stop replacement process. str.replacerx will return immediately. It returns number of replacements not including current, or -1 for single replacement mode. To stop replacement process and include current replacement, set x.rr.ito = 0.
;< -100 generate error with this error number.


x.match.ucase
#3
Ok. Not quite there yet, but so close.

So I have things like this:
"Tuesday-Friday 9:00 a.m. to 9:00 p.m."
"Tuesday-Friday 9:00 am to 9:00 pm"
"Tuesday-Friday 9:00 AM to 9:00 PM"

I want it to be:
Tuesday-Friday 9:00AM to 9:00PM

With the callback function it matches the whole thing, when I want to just do submatches where only the "a" and "p" are matched.
#4
I replaced the example for AM/PM.


Forum Jump:


Users browsing this thread: 1 Guest(s)