Convert word to title case

Find, replace, find in files, replace in files, regular expressions

Convert word to title case

Postby UltraEFan » Wed Dec 14, 2011 3:49 pm

Hello,

I have been trying to get a regex pattern that will convert to title case words inside:

<title> MY TEST TITLE</title>.

The resulting content should be like:

<title>My Test Title</title>.


Thanks.
UltraEFan
Newbie
 
Posts: 7
Joined: Thu Oct 27, 2011 10:22 am

Re: Convert word to title case

Postby Mofi » Thu Dec 15, 2011 7:14 am

Well, it is possible with a Perl regular expression to change the case of letters of a word. But it is not possible to find the title tag and capitalize all words between the tags. That's the same problem as with inserting <p> within a notes tag. This is a 2 step process: first find the text between tags, second find the words within the selection and make the first character upper case and all other lower case.

So you need again a macro for this task. I have to offer 2 possible solutions. The first one use normal finds to select the text between the tags (plus </ of closing title tag) and use UltraEdit command Format - Capitalize.

InsertMode
ColumnModeOff
HexOff
PerlReOn
Find "<title"
Key RIGHT ARROW
StartSelect
Find Select "</"
ToCaps
EndSelect


The second macro is the same as above, but instead of ToCaps following Perl regular expression replace is used.

Find RegExp SelectText "\<(\w)(\w*)\>"
Replace All "\U\1\E\L\2\E"
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Convert word to title case

Postby UltraEFan » Thu Dec 15, 2011 9:33 am

Hello Mofi,

I tried the macro and it seems to work with the ToCaps, but it did not replace all instances in all <title></title> in the open document, it did change only all words in one instance of <title></title>. Is that normal?

When I tried with the perl replace all option, it did the same, replacing only all words in one instance of <title></title>. I thought with replace all, it should be able to replace all words in all instance of <title></title>?

This is the macro I used with the perl option, I am doing something wrong?

InsertMode
ColumnModeOff
HexOff
PerlReOn
Find "<title"
Key RIGHT ARROW
StartSelect
Find Select "</"
Find RegExp SelectText "\<(\w)(\w*)\>"
Replace All "\U\1\E\L\2\E"
UltraEFan
Newbie
 
Posts: 7
Joined: Thu Oct 27, 2011 10:22 am

Re: Convert word to title case

Postby Mofi » Thu Dec 15, 2011 11:21 am

A HTML file contains the <title> tag only once (or better it should contain it only once at top of the file in the head section). The macro is not written for doing that on multiple <title> tags within a file. You need to wrap a loop around the lines with an exit condition if your files contain multiple title tags.

InsertMode
ColumnModeOff
HexOff
PerlReOn

Top
Loop 0

Find "<title"
IfNotFound
ExitLoop
EndIf

Key RIGHT ARROW
StartSelect
Find Select "</"
Find RegExp SelectText "\<(\w)(\w*)\>"
Replace All "\U\1\E\L\2\E"

EndSelect
EndLoop
Top


Parameter SelectText is like selecting Selected Text in the Replace dialog for Replace Where. It limits the replace to the selected text. Otherwise the Perl regular expression Replace All would capitalize all words in the entire file beginning from current position of the caret to bottom of file.

By the way: ToCaps is better than the Perl method because the Perl method supports only letters A-Za-z and no local specific case changes like for the German umlauts ÄäÖöÜü as ToCaps supports. Well, if your text is in English, the limited case change capabilities of the Perl engine does not matter.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Convert word to title case

Postby UltraEFan » Thu Dec 15, 2011 11:32 am

Thanks Mofi,

This definitely answers my needs.

The legacy document some times may contain more than one <title></title>, and then it gets broken/split into more than one XML file afterwards.
UltraEFan
Newbie
 
Posts: 7
Joined: Thu Oct 27, 2011 10:22 am

Re: Convert word to title case

Postby UltraEFan » Thu Dec 15, 2011 11:39 am

Mofi,

One more question, is it possible to get this macro to work on multiple files in a folder? I tried the other macro you directed me to in one of my earlier post but for some reason it did not work.

Thanks again for your help.
UltraEFan
Newbie
 
Posts: 7
Joined: Thu Oct 27, 2011 10:22 am

Re: Convert word to title case

Postby Mofi » Thu Dec 15, 2011 1:05 pm

I have just added additional information to my post Run Macro on all files within folder. So please read it again. Step by step instruction for running this macro on all files in a directory.

  1. Open Advanced - Configuration - Search - Set Find Output Format, verify that setting Find Summary is checked and that the string starts with Search complete, found. If you are using a non English version of UltraEdit, remember what are the first words of this string (case sensitive!). It is not necessary to use the English string here in the configuration. Close the configuration dialog with Cancel (if nothing changed) or OK (if setting enabled).
  2. Click on Macro - Delete All to delete all currently loaded macros in the macro buffer. This step is not really necessary, but it makes the step by step instruction for me easier to write.
  3. Click on Macro - Edit Macro.
  4. Click on button New Macro.
  5. Enter as macro name CapitalizeTitles. Uncheck macro property Show Cancel dialog for this macro. The property Continue if search string not found remains checked. Press button OK.
  6. Switch to your browser, copy following lines into Windows clipboard, select all lines in Edit/Create Macro dialog and press Ctrl+V to replace them by the lines below.

    Code: Select all
    InsertMode
    ColumnModeOff
    HexOff
    PerlReOn
    Top
    Loop 0
    Find "<title"
    IfNotFound
    ExitLoop
    EndIf
    Key RIGHT ARROW
    StartSelect
    Find Select "</"
    ToCaps
    EndSelect
    EndLoop
    Top

  7. Make sure there is no empty line at end of the macro source becaues that would be interpreted by UltraEdit as incorrect macro command.
  8. Click again on button New Macro.
  9. Enter as macro name CapTitlesInFiles. Uncheck macro property Show Cancel dialog for this macro. The property Continue if search string not found remains checked. Press button OK.
  10. UltraEdit asks you now if you want to update the modified macro CapitalizeTitles. Confirm that with clicking on button Yes.
  11. Switch to your browser, copy following lines into Windows clipboard, select all lines in Edit/Create Macro dialog and press Ctrl+V to replace them by the lines below.

    Code: Select all
    FindInFiles "directory" "file type" ""
    Loop
    Find MatchCase Up "Search complete, found"
    IfFound
    ExitLoop
    Else
    NextWindow
    EndIf
    EndLoop
    DeleteLine
    SelectToBottom
    IfSel
    Delete
    EndIf
    Top
    UnicodeToASCII
    Loop
    IfEof
    ExitLoop
    EndIf
    StartSelect
    Key END
    IfSel
    Open "^s"
    PlayMacro 1 "Name of your macro"
    CloseFile Save
    EndIf
    EndSelect
    DeleteLine
    EndLoop
    CloseFile NoSave

  12. Change on the first line the directory string to the path of the directory containing your files. The path must end with a backslash.
  13. Also on first line change the file type string to *.* or *.xml or whatever is best for updating only the files with the title tags.
  14. If the macro should run on all files of a directory tree instead of just all files within the specified directory, insert on first line between the command FindInFiles and the directory string the parameter keyword Recursive (with an additional space between this word and the double quote character of the directory string).
  15. If you are not using English version of UltraEdit or you have already customized the find summary string, adapt the string Search complete, found to your string, refer to point 1 of the list.
  16. Replace Name of your macro and the PlayMacro command line with CapitalizeTitles as this is the name of the just before created macro.
  17. Make sure there is no empty line at end of the macro source becaues that would be interpreted by UltraEdit as incorrect macro command.
  18. Click on button Close.
  19. Confirm that you want to update the macro CapTitlesInFiles with clicking on button Yes.
  20. Use Macro - Save All to save these 2 macros into a macro file.
Now the 2 macros are ready for execution. The last created macro CapTitlesInFiles is automatically selected as active macro. Therefore you only need to click on Macro - Play Again to execute and it watch how UltraEdit updates now your files one after the other.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Convert word to title case

Postby UltraEFan » Thu Dec 15, 2011 2:39 pm

Mofi,

Thank you very much for these detailed steps, I will report back regarding the result.
UltraEFan
Newbie
 
Posts: 7
Joined: Thu Oct 27, 2011 10:22 am


Return to Find/Replace/Regular Expressions