Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex ucase replacement
#1
Hey folks...been a while and I hope y'all are doing swell!

Trying to camel case some text by removing spaces and capitalizing the letter that comes afterwords:

Function camelCase
Code:
Copy      Help
str Text="hello world"
Text.replacerx(" (.{1})" "\u$1")
out Text

I found this stackoverflow page that shows using the "\u" sort of thing, but in QM it outputs "hello\uworld" instead of "helloWorld"
https://stackoverflow.com/questions/2074...s/20742304

This works...but it's not very pretty:
Function camelCase
Code:
Copy      Help
str s="hello world"
REPLACERX r
r.frepl=&sub.replacerx_callback
s.replacerx(" (.{1})" &r)
out s

#sub replacerx_callback
function# REPLACERXCB&x
x.match.trim
x.match.ucase
#2
QM regex replacement does not support \u.
Could use callback, but problem with non-ASCII characters.
This function does not use regex.

Member function str.TitleCase
Code:
Copy      Help
;Makes the first character of every word uppercase.
;As word separators recognizes only ASCII whitespace (space, tab, newlines).
;Supports all Unicode word characters.

;EXAMPLE
;str s="hello world ąčę"
;s.TitleCase
;out s ;;Hello World Ąčę


if(!this.len) ret
BSTR b=this
int i
for i 0 b.len
,if i=0 or b[i-1]<=32
,,if b[i]>='a'
,,,_i=b[i]
,,,b[i]=CharUpperW(+_i)
this=b
#3
Yes that will work. Just title case it, then remove all the spaces. Nice.

Good to know QM does not support /u...I was thinking I was doing something wrong Wink
#4
Running this causes this error:
Quote:'Error in <open ":3115: /268">test:  'this' can be used only in class member function

What further editing does this script require?
#5
You need to create a new member function
on qm editor window 
click file, then new then new member function
name it str.TitleCase
then paste code from post above(click the copy link above the post and paste into this new function

if you are still unsure just download this file
open it and click import on qml fileviewer window

.qml   str.TitleCase.qml (Size: 3 KB / Downloads: 219)

   
#6
I imported the file, but nothing happens when I run the script.

what am I missing?
#7
you call it from another function or macro
 the member function is not run using the run button. It is only called from code
example

Macro Macro1
Code:
Copy      Help
str s="hello world ąčę"
s.TitleCase
out s ;;Hello World Ąčę
#8
Ahhh, ok, now I understand. Thanks so much for the assist!!

Is there a similar function that would allow for just capitalizing the first word in a string?
#9
Macro Macro3
Code:
Copy      Help
str s="hello world ąčę"
int i=findc(s 32 0)
s.ucase(0 i)
out s
#10
Great stuff! Thank you, Kevin for all your help tonight.  Smile


Forum Jump:


Users browsing this thread: 1 Guest(s)