Jump to content

Capitalize words


Recommended Posts

Hello,

I want to capitalize 1st word of every sentence with StringRegExp, I am able to collect words by using this pattern:

Local $reg = '(?:^|(?:[.!?]\s))(\w+)'

now my problem is how can I exactly replace these words, e.g. In the following string:

the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog.

I only want to replace 1st "the" of the sentence with "The" but I have only words in array from RegExp, without its position in string.

Thanks.
 

73 108 111 118 101 65 117 116 111 105 116

Link to comment
Share on other sites

Try this:

#include <String.au3>

$sSentence = "the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog."
ConsoleWrite(_StringProper(StringRegExpReplace($sSentence, "(\w+)\h.*", "$1")) & StringRegExpReplace($sSentence, "\w+(\h.*)", "$1") & @CRLF)

Not very elegant...

Edit: this works only for one sentence! o:)

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

This will work over all Unicode:

Local $s =  "the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog." & @CRLF & _
            "another sentence. yet another sentence. фраза на русском. The end."
$s = Execute('"' & StringRegExpReplace($s, '(*UCP)(^|\.)(\s*\p{Ll})', '$1" & StringUpper("$2") & "') & '"')
MsgBox(0, '', $s)

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

No, it doesn't give the right capitalization is some languages. This is more correct:

$s = Execute('"' & StringRegExpReplace($s, '(*UCP)(^|\.)(\s*\X)', '$1" & StringUpper("$2") & "') & '"')

Note that \X matches any EGC (extended grapheme cluster) regardless of case, so it will invoke StringUpper even for letters or EGC already capitalized contrary to the previous version, but this I regard as a minor inconvenience.

This version is still perfectible in that it only recognizes the dot . for end of sentence. But several languages use other symbols instead of dot.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

No, it doesn't give the right capitalization is some languages. This is more correct:

$s = Execute('"' & StringRegExpReplace($s, '(*UCP)(^|\.)(\s*\X)', '$1" & StringUpper("$2") & "') & '"')

Note that \X matches any EGC (extended grapheme cluster) regardless of case, so it will invoke StringUpper even for letters or EGC already capitalized contrary to the previous version, but this I regard as a minor inconvenience.

This version is still perfectible in that it only recognizes the dot . for end of sentence. But several languages use other symbols instead of dot.

​Thank you very much, I only require it for English language. I slightly modified your pattern "(*UCP)(?:^|(\.|!|\?))(\s*\X)" and its working as expected. Thanks Again.

73 108 111 118 101 65 117 116 111 105 116

Link to comment
Share on other sites

OK

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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

×
×
  • Create New...