Sign in to follow this  
Followers 0
philw

Regular expression question

5 posts in this topic

I'm trying to write a script that will check a block of text and capitalize the first letter of sentences if required. Something like:

$str=StringRegExpReplace($teststr, "\. ([a-z])", "\. \1")

except that the back-reference needs to be converted to uppercase. Is this possible?

Thanks!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

StringUpper()

maybe? i dunno how that would work :P

Edited by Paulie

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Yeah, that's just not going to happen the way you have it without a loop. I thought we had a _StringProper function?

Edit.. Example:

$s_text = "this is a test.  only to see if it works." & @CRLF & @TAB & "if it doesn't work, oh well."
$s_convert = _StringProperSentence($s_text)
MsgBox(0, 0, $s_convert)

Func _StringProperSentence($s_string)
    Local $a_sre = StringRegExp($s_string, "\s*(.)(.+?\.\s*)", 3)
    If @error Then Return SetError(1, 0, $s_string)
    Local $s_hold
    For $i_cc = 0 To UBound($a_sre) - 1 Step 2
        $s_hold &= StringUpper($a_sre[$i_cc]) & $a_sre[$i_cc + 1]
    Next
    Return $s_hold
EndFunc
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

If you could use the output of the "StringUpper" function in the replace pattern, this would work great:

(I added "<upper>" & "</upper>" to see what should be getting translated to uppercase)

$s_text = "this is a test.  only to see if it works." & @CRLF & "if it doesn't work, oh well."
$s_convert = StringRegExpReplace($s_text, "(?m)(^|(\.  *))([a-z])", "\2<upper>" & StringUpper("\3") & "</upper>")
MsgBox(0, 0, $s_convert)

This is what gets output:

<upper>t</upper>his is a test.  <upper>o</upper>nly to see if it works.
<upper>i</upper>f it doesn't work, oh well.

Dan

Share this post


Link to post
Share on other sites

If you could use the output of the "StringUpper" function in the replace pattern, this would work great:

(I added "<upper>" & "</upper>" to see what should be getting translated to uppercase)

$s_text = "this is a test.  only to see if it works." & @CRLF & "if it doesn't work, oh well."
$s_convert = StringRegExpReplace($s_text, "(?m)(^|(\.  *))([a-z])", "\2<upper>" & StringUpper("\3") & "</upper>")
MsgBox(0, 0, $s_convert)

This is what gets output:

<upper>t</upper>his is a test.  <upper>o</upper>nly to see if it works.
<upper>i</upper>f it doesn't work, oh well.

Dan

You can't use an outside function within the function. Functions are fired from right to left. So StringUpper() would fire before you even did the regular expression. Thus my statement on.. It can't be done that way.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0