Jump to content

Regular expression question


 Share

Recommended Posts

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!

Link to comment
Share on other sites

  • Moderators

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.

Link to comment
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

Link to comment
Share on other sites

  • Moderators

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...