This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CastleMan2000's profile picture

Help with regexp?

Started by CastleMan2000, 09 February 2013 - 02:31 PM
CastleMan2000 #1
Posted 09 February 2013 - 03:31 PM
I know, this is a forum for CC/lua, but I really have nowhere else to ask this: I am trying to format a string with regexp. I have never used regexp before. What I want is for the first two letters of the string to be capitalized, but all I can find is the anchor for the beginning of a string. How can I select the first two letters of the string, and then capitalize them?
GravityScore #2
Posted 09 February 2013 - 04:06 PM
You don't need regex for that.


s = "heLlO TheRe"
s = s:sub(1, 2):upper() .. s:sub(3):lower()
CastleMan2000 #3
Posted 09 February 2013 - 04:54 PM
Um. Actually, I do need regex. It's kind of a scripting thing on an application. Specifically, I'm using Pesterchum and trying to implement a quirk. The quirk system, along with several other preinstalled choices, has a quirk option for regex scripting.

Regex: *text box*
Replace with: *text box*

It is supposed to work like this:

CB: HEy dude. HOw's it going? STuff like that. LIke someone holding the shift key a bit too long.

[attachment=998:Screen shot 2013-02-08 at 11.07.06 PM.png]

See? Regexp is mandatory.
Kingdaro #4
Posted 09 February 2013 - 05:06 PM
I'm not sure how to turn something uppercase with regex alone, but I do know how to capture/reuse matches. It's $1, $2, $3 etc. like %1, %2, %3 etc. in lua.

So replacing "l" with "$1…" in the string "Hello World!" would produce "Hel…l…o Worl…d!"
Xtansia #5
Posted 09 February 2013 - 06:08 PM
Just took a look at pesterchum and this seems to be what you want:

Regexp: \b(\w\w|\w)
Replace: upper(\1)

And if you want only the first 2 letters of a word capitilized then you need this one aswell but applied before the above one
Regexp: (.)
Replace: lower(\1)
CastleMan2000 #6
Posted 10 February 2013 - 04:16 AM
Well, I didn't need the second one, but otherwise it worked well. Except for one thing:
CB: IT TYped LIke THis
CB: NOt like this.

It capitalized the first two letters of every word, not sentence.
CastleMan2000 #7
Posted 10 February 2013 - 09:45 AM
Hello? This still hasn't been resolved.
Xtansia #8
Posted 10 February 2013 - 03:47 PM
Doing the first 2 letters of every line is pretty simple the regexp would just be
Regexp: ^(\w\w|\w)
Replace: upper(\1)

But matching multiple sentences on one line not so much.
Kingdaro #9
Posted 10 February 2013 - 04:56 PM
To match the first two letters of every sentence, then lowercase the rest:

Match: (\w\w)(.*?\.)
Replace: upper(\1)lower(\2)

For just the two letters, without lowercasing the rest:

Match: (\w\w).*?\.
Replace: upper(\1)lower(\2)

Untested, so I'm not completely sure if this'll work.
CastleMan2000 #10
Posted 10 February 2013 - 05:48 PM
Doing the first 2 letters of every line is pretty simple the regexp would just be
Regexp: ^(\w\w|\w)
Replace: upper(\1)

But matching multiple sentences on one line not so much.

Thank you! This works perfectly :D/>

Given that it's a chat client, I probably won't really be having more than one sentence per line. So it won't be a problem! :)/>
TeddyJ #11
Posted 17 February 2013 - 09:51 AM
I find this site is really helpful when I'm trying to thrash out regexes http://regexpal.com/