Jump to content
Sign in to follow this  
abberration

StringLower not working in StringRegExpReplace

Recommended Posts

Hi all,

Say I want to capitalize every word in a string and words like would've come out as Would'Ve. Using StringRegExpReplace, I have the code back referencing the word as three pieces (before the apostrophe, the apostrophe and after). When reconstructing the word, I tried to use StringLower to make the part after the apostrophe lowercase. It is not working. By the way, I did not apply this to words starting with O because of names like O'Malley, as seen in the code below. Any suggestions?

#include <String.au3>

$string = "i'm would've O'Malley o'malley can't"
$string = _stringproper($string)
$string2 = StringRegExpReplace($string, "([A-N|P-Z])(')([A-Z])", "$1$2" & StringLower("$3"))

MsgBox(0, "", $string & @CRLF & $string2)


 

Share this post


Link to post
Share on other sites

a few, but they start with the regex. 

-Even the lower succeeded, I think you want to capture more than the first M.

-If you search for [only capital characters except for o] preceding the apostrophe you will never catch "Would'Ve" or "Can'T"

once you can get dashes around all the items, then we can act on those items.

#include <String.au3>

$string = "i'm would've O'Malley o'malley can't"
$string = _stringproper($string)
$string2 = StringRegExpReplace($string, "([A-N|P-Z])(')([A-Z])", "$1$2" & "-$3-")

MsgBox(0, "", $string & @CRLF & $string2)

 

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Thank you for your suggestions. I see a flaw in the part about affecting only words that do not start with O. Anything with an O will not be affected by the script. I just found _StringTitleCase, which is closer to what I want than _StringProper. However, going at in reverse and only targeting words that start with O and capitalizing the part on the right of the apostrophe, I have the same problem.

Sorry, I don't understand why I would need to use dashes around the items. Nor do I see why I need to look at more than the first letter after the apostrophe. Here's my new, simpler method (still not working properly).

#include <String.au3>

$string = "i'm would've O'Malley o'malley can't"
$string = _StringTitleCase($string)
$string2 = StringRegExpReplace($string, "([O])(')([a-z])", "$1$2" & StringUpper("$3"))

MsgBox(0, "", $string & @CRLF & $string2)

 

Share this post


Link to post
Share on other sites

I now see it won't work the way I want. I wrote a function and passed the "$3" to it and looked at the ASC value and it came up with 32, which is the $ of the passed value (it is passing a literal $3 instead of the intrinsic value of the third chunk of regular expression). I began working on a function to process everything in a different way. I'll post final code when I get it. I'm burned out now, so I'll work on it tomorrow.

I'm still a bit confused why stringregexpreplace still put in the third chunk in when calling it through a stringupper or _stringproper (but not capitalized). More mysteries to delve into...

Thanks to all minds who gave this some thought.

Peace!

Share this post


Link to post
Share on other sites

search "execute" & "stringregexpreplace" & "mikell" & "JGunich"

in the meanwhile, is this the desired outcome?  Aside from the larger learning aspect, i would not use regex with the criteria provided:

#include <String.au3>

$string = "i'm would've O'Malley o'malley can't"
msgbox(0, '' ,  stringreplace(_StringTitleCase($string) , "O'm" , "O'M"))

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Is that what you're after?

Local $string = "*** gzno(uà 156+ more garbage *** i'm would've O'Malley o'malley can't they'VE chance!"
Local $string2 = Execute('"' & StringRegExpReplace($string, "(?ix) (?| (O'[[:alpha:]]+) () | ([[:alpha:]]+'?) ([[:alpha:]]*))", '" & _StringProper("$1") & StringLower("$2") & "') & '"')
MsgBox(0, "", $string & @LF & $string2)

A little addition even handles McDonald's and even more McO'Tool's friend correctly(*):

Local $string = "*** gzno(uà mcdonald mco'tool mco'tool's friend mcdONald'S junk food 156+ more garbage *** i'm would've O'Malley o'malley can't they'VE chance!"
Local $string2 = Execute('"' & StringRegExpReplace($string, "(?ix) (?| (O'[[:alpha:]']+) () | (Mc) () (?=[[:alpha:]]+) | (?<=Mc) ([[:alpha:]]+) () | ([[:alpha:]]+'?) ([[:alpha:]]*))", '" & StringUpper(StringLeft("$1", 1)) & StringLower(StringTrimLeft("$1", 1)) & StringLower("$2") & "') & '"')
MsgBox(0, "", $string & @LF & @LF & $string2)

(*) Until a non-intuitive counter-example surfaces...

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)

Share this post


Link to post
Share on other sites
10 hours ago, iamtheky said:

search "execute" & "stringregexpreplace" & "mikell" & "JGunich"

in the meanwhile, is this the desired outcome?  Aside from the larger learning aspect, i would not use regex with the criteria provided:

#include <String.au3>

$string = "i'm would've O'Malley o'malley can't"
msgbox(0, '' ,  stringreplace(_StringTitleCase($string) , "O'm" , "O'M"))

 

Just a quick thanks for the response. I will study up on "execute", which I see jchd used in his example below. And I will look for mikell and jgunich's posts on similar topics. Your example is interesting, but wouldn't cover other things beginning with O such as O'Darby or 3 O'Clock.

5 hours ago, jchd said:

Is that what you're after?

Local $string = "*** gzno(uà 156+ more garbage *** i'm would've O'Malley o'malley can't they'VE chance!"
Local $string2 = Execute('"' & StringRegExpReplace($string, "(?ix) (?| (O'[[:alpha:]]+) () | ([[:alpha:]]+'?) ([[:alpha:]]*))", '" & _StringProper("$1") & StringLower("$2") & "') & '"')
MsgBox(0, "", $string & @LF & $string2)

A little addition even handles McDonald's and even more McO'Tool's friend correctly(*):

Local $string = "*** gzno(uà mcdonald mco'tool mco'tool's friend mcdONald'S junk food 156+ more garbage *** i'm would've O'Malley o'malley can't they'VE chance!"
Local $string2 = Execute('"' & StringRegExpReplace($string, "(?ix) (?| (O'[[:alpha:]']+) () | (Mc) () (?=[[:alpha:]]+) | (?<=Mc) ([[:alpha:]]+) () | ([[:alpha:]]+'?) ([[:alpha:]]*))", '" & StringUpper(StringLeft("$1", 1)) & StringLower(StringTrimLeft("$1", 1)) & StringLower("$2") & "') & '"')
MsgBox(0, "", $string & @LF & @LF & $string2)

(*) Until a non-intuitive counter-example surfaces...

Wow, that is a fairly complicated (but effective) solution. I'm still learning regex and I understand most of what you posted. I do need to understand the purpose of execute, which I will study up on. Nice addition of words beginning with Mc. I hadn't even thought of that. I need to break down your code to understand why stringupper and stringlower worked for you. Thanks for the code. It will be a useful learning tool.

Share this post


Link to post
Share on other sites

And jchd, of course. The adjective, as in “you really jchd’d the shit out of that regex”.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...