Digisoul Posted June 16, 2015 Share Posted June 16, 2015 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 More sharing options...
CaptainSparky Posted June 16, 2015 Share Posted June 16, 2015 You could experiment with StringUpper() Hello. If in someway I have helped, please consider liking my post(s). The formula for the right answer: You + Right Question = Answer (Think) BEFORE you post. Link to comment Share on other sites More sharing options...
UEZ Posted June 16, 2015 Share Posted June 16, 2015 (edited) 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! Edited June 16, 2015 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 More sharing options...
jchd Posted June 16, 2015 Share Posted June 16, 2015 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
UEZ Posted June 16, 2015 Share Posted June 16, 2015 That's cool jchd! 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 More sharing options...
jchd Posted June 16, 2015 Share Posted June 16, 2015 (edited) 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 June 16, 2015 by jchd Digisoul 1 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
Digisoul Posted June 16, 2015 Author Share Posted June 16, 2015 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 More sharing options...
jchd Posted June 16, 2015 Share Posted June 16, 2015 (edited) OK Edited June 16, 2015 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now