Jump to content

Uppercase in part of string using RegEx


remin
 Share

Recommended Posts

I can't find out how to convert a part of a string to uppercase.

 

p.e. 

$Clipboard = StringLower(ClipGet())

 

 

;------------- ----------------------my AHK CODE ---------------------------------

; Title Case only if word is: 1) at Start of line OR 2) preceding with a space

; Clipboard := RegExReplace(Clipboard, "(^|s)(w)", "$1$U2")

 

; Same as above but only for words > 1 character

; Clipboard := RegExReplace(Clipboard, "(^|s)(w)(w+)", "$1$U2$3")

 

: Sentence Case

; Clipboard := RegExReplace(Clipboard, "(((^|([.!?]+s+))[a-z])| i | i')", "$u1")

;------------- ----------------------end AHK CODE ---------------------------------

 

$U2 means in autohotkey that you convert the 2nd caption to Uppercase

 

Can anyone tell me how to do above Replacements in autoit?

It cannot be done dynamically as in autohotkey (using $U2 etc), isn't it?

 

Every help is appreciated :)

Edited by Melba23
Added SRE to title
Link to comment
Share on other sites

  • Moderators

remin,

There is (as far as I know) no equivalent to the $U# syntax in AutoIt, :(

But I am sure the SRE gurus (not me) can come up with something - stick around and see what happens. ;)

M23

Edit: I have added SRE to the thread title to attract them. ;)

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Maybe something like this.

Local $sStr = ClipGet()
Local $sStr = "a title case only if a word is at start of line.  or is preceding with a space."

; Title Case only if word is: 1) at Start of line OR 2) preceding with a space
ConsoleWrite(Execute('"' & StringRegExpReplace($sStr, "\b([a-z])", '" & StringUpper("\1") &  "') & '"') & @LF)
;Returns: A Title Case Only If A Word Is At Start Of Line.  Or Is Preceding With A Space.

; Same as above but only for words > 1 character
ConsoleWrite(Execute('"' & StringRegExpReplace($sStr, "\b([a-z])([a-z]+)", '" & StringUpper("\1") & "\2" & "') & '"') & @LF)
;Returns: a Title Case Only If a Word Is At Start Of Line.  Or Is Preceding With a Space.

; Sentence Case
ConsoleWrite(Execute('"' & StringRegExpReplace($sStr, "(\A|\.\s+)([a-z])", '" & "\1" & StringUpper("\2") & "') & '"') & @LF)
;Returns: A title case only if a word is at start of line.  Or is preceding with a space.
Link to comment
Share on other sites

A little suggestion in case of hyphens

Local $sStr = "autoit is a freeware basic-like scripting language. most people should be able to pick it up easily "

$txt = "Title Case only if word is: 1) at Start of line OR 2) preceding with a space"
;$res = Execute('"' & StringRegExpReplace($sStr, '\b([a-z])', '" & StringUpper("$1") &  "') & '"')

$res = Execute('"' & StringRegExpReplace($sStr, '\b((?<!\-)[a-z])', '" & StringUpper("$1") &  "') & '"')
Msgbox(0,"", $txt  &@crlf&@crlf& $res)
Link to comment
Share on other sites

Hi Malkey,

Thank you for writing.

However, I can't make it work.

The output is the same as the input.

With all three regex you gave

(and it takes a lot of time to return the string I noted, is that correct?).

I just copied above answer and tried it with the clipboard content and the string you gave.

ps: tnx Melba for adding SRE to my message :)

Link to comment
Share on other sites

There's a _StringTitleCase function in the newest beta version, doesn't work with Unicode very well, but it will capitalize the first character of every word.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

 

A little suggestion in case of hyphens

$res = Execute('"' & StringRegExpReplace($sStr, '\b((?<!\-)[a-z])', '" & StringUpper("$1") &  "') & '"')

 

Hi Mikell,

Thank you.

It works fine.

I noted that it doesn't work with all ascii characters.

I tried to change it to:

$res = Execute('"' & StringRegExpReplace($sStr, '\b((?<!\-)[a-zà-ýœß])', '" & StringUpper("$1") &  "') & '"')

I thought it was the solution but it simply stepped over characters as èéàáùìò..

école ==> éCole

può ==> PuÒ

Edited by remin
Link to comment
Share on other sites

Local $sText = "a title case only if a word is at start of line.  or is preceding with a space."
$iOffset = 1
Do
    $aRes = StringRegExp($sText, '\b[a-z]', 1, $iOffset)
    If @error Then ExitLoop
    $iOffset = @extended
    $sText = StringReplace($sText, $iOffset - 1, StringUpper($aRes[0]))
Until 0
MsgBox(0, 'Yes?', $sText)

Link to comment
Share on other sites

Found the solution with the help of Mikell's answer:

 ;title case
 $result = Execute('"' & StringRegExpReplace($ClipB, "(^|\s|\n^)([a-zà-ýœß])", '$1" & StringUpper("$2") &  "') & '"')

 ;title case > 1 character
 $result = Execute('"' & StringRegExpReplace($ClipB, "(^|\s|\n^)([a-zà-ýœß])([a-zà-ýœß]+)", '$1" & StringUpper("$2") & "$3') & '"')

 ;sentence case
 $result = Execute('"' & StringRegExpReplace($ClipB, "(((^|([.!?]+\s+))[a-zà-ýœß])| i | i')", '" & StringUpper("$1") &  "') & '"')

Thank you all for your answers and your help  :thumbsup:

Edited by remin
Link to comment
Share on other sites

Why in the world would you EVER use Execute for something like that?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

My only point was about wrapping the code inside an Execute statement when it clearly isn't needed.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@remin

Take into account any letters

Local $sText = "a title case only if a word is at start of line.  or is preceding with a space."
$iOffset = 1
Do
    $aRes = StringRegExp($sText, '(?<![\x{41}-\x{ffff}])[\x{41}-\x{ffff}]', 1, $iOffset)
    If @error Then ExitLoop
    $iOffset = @extended
    $sText = StringReplace($sText, $iOffset - 1, StringUpper($aRes[0]))
Until 0
MsgBox(0, 'Yes?', $sText)
Link to comment
Share on other sites

That's pretty close to the _StringTitleCase function.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

 

Found the solution with the help of Mikell's answer:

 ;title case
 $result = Execute('"' & StringRegExpReplace($ClipB, "(^|\s|\n^)([a-zà-ýœß])", '$1" & StringUpper("$2") &  "') & '"')

 ;title case > 1 character
 $result = Execute('"' & StringRegExpReplace($ClipB, "(^|\s|\n^)([a-zà-ýœß])([a-zà-ýœß]+)", '$1" & StringUpper("$2") & "$3') & '"')

 ;sentence case
 $result = Execute('"' & StringRegExpReplace($ClipB, "(((^|([.!?]+\s+))[a-zà-ýœß])| i | i')", '" & StringUpper("$1") &  "') & '"')

Thank you all for your answers and your help  :thumbsup:

 

 

I thought that it was the solution but it doesn't work when there are quotes in the text.

p.e.

This is a "test"

It returns an empty string.

Anyone knows why it doesn't accept quotes?

Link to comment
Share on other sites

Have you looked at the _StringTitleCase function to see how it is done there. Instead of trying to do it all in one shot, it might take more work.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Quotes are a big trouble in regexes, often I manage this the ass-way by the use of 'Replaces' :idiot:

Local $sStr = 'autoit is a freeware "basic-like" scripting language. most people should be able to pick it up easily '

$sStr = StringReplace($sStr, '"', '§')
$res = Execute('"' & StringRegExpReplace($sStr, '\b((?<!\-)[a-z])', '" & StringUpper("$1") &  "') & '"')
$res = StringReplace($res, '§', '"')
Msgbox(0,"", $res)

BrewManNH of course you're right but isn't it nice to be able to get solutions in case of slightly different circumstances ?

Link to comment
Share on other sites

Except that RegEx has the same problem that the _StringProper function does, it doesn't treat a single quote (apostrophe) correctly. Change your string to this and you'll see what I mean.

Local $sStr = "autoit's a freeware ""basic-like"" scripting language. most people (should) be able to pick its up easily"

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Hmm yes I understand what you mean

Local $sStr = "autoit's a freeware ""basic-like"" scripting language. most people (should) be able to pick it up easily"

$sStr = StringReplace($sStr, "'", '¤a')
$sStr = StringReplace($sStr, '"', '§')
$res = Execute('"' & StringRegExpReplace($sStr, '\b((?<!\-|\()[a-z])', '" & StringUpper("$1") &  "') & '"')
$res = StringReplace($res, '§', '"')
$res = StringReplace($res, '¤a', "'")

It remains manageable (I think so...) but becomes really tricky

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...