yoinkster Posted April 5, 2011 Share Posted April 5, 2011 Hey guys, I think this will just be a quick one but I can't figure it out. What I'd like to do is pretty simple: I want to take a string, find if has a substring of the format "character-character" and then replace it with "character - character". So I figured I'd need to use StringRegExpReplace. Here's what I have so far: $string = "first-second" $string = StringRegExpReplace($string, "[^\s]-[^\s]"," - ") and this gives me, unsurprisingly, "firs - econd" and I know why it does that, but I can't figure out how to make it return "first - second" Any help would be greatly appreciated, AC. Link to comment Share on other sites More sharing options...
MrMitchell Posted April 5, 2011 Share Posted April 5, 2011 (edited) Something like this: $string = StringRegExpReplace($string, "([^\s])-([^\s])","\1 - \2") Edit: Nevermind I have the wrong idea Edited April 5, 2011 by MrMitchell Link to comment Share on other sites More sharing options...
Aktonius Posted April 5, 2011 Share Posted April 5, 2011 Something like this: $string = StringRegExpReplace($string, "([^\s])-([^\s])","\1 - \2") Edit: Nevermind I have the wrong idea Think you have right idea Link to comment Share on other sites More sharing options...
MrMitchell Posted April 5, 2011 Share Posted April 5, 2011 (edited) Looking at what you were trying to do, this works: $input = "first-second" ConsoleWrite($input & @CRLF) $output = StringRegExpReplace($input, "-"," - ") ConsoleWrite($output & @CRLF) But it could get more complicated if it's not a straight up replace all hypens with "space hyphen space...actually could just use StringReplace() instead lol Edited April 5, 2011 by MrMitchell Link to comment Share on other sites More sharing options...
yoinkster Posted April 5, 2011 Author Share Posted April 5, 2011 (edited) Something like this: $string = StringRegExpReplace($string, "([^\s])-([^\s])","\1 - \2") Edit: Nevermind I have the wrong idea This is exactly what I wanted, thank you Looking at what you were trying to do, this works: $input = "first-second" ConsoleWrite($input & @CRLF) $output = StringRegExpReplace($input, "-"," - ") ConsoleWrite($output & @CRLF) There are more complicated ways... This would have worked yes, but I didn't want end up having for example "first - second" (one space) becoming "first - second" (two spaces) hence wanting to find a not-space character Edited April 5, 2011 by yoinkster Link to comment Share on other sites More sharing options...
MrMitchell Posted April 5, 2011 Share Posted April 5, 2011 (edited) The thing is according to the help file it would be \0 and \1 instead of \1 and \2, but no matter what I did I didn't get expected results. I don't have much experience with these haha (Another) Edit: I guess \1 and \2 is what you want, cuz \0 is the entire thing then \1 and \2 are the first and second set of parentheses. Edited April 5, 2011 by MrMitchell Link to comment Share on other sites More sharing options...
yoinkster Posted April 5, 2011 Author Share Posted April 5, 2011 Ah ok, that makes sense, thanks for the explanation. One day I'll learn these things properly instead of just bodging it all the time! Link to comment Share on other sites More sharing options...
GEOSoft Posted April 5, 2011 Share Posted April 5, 2011 Also it's a good idea to use $1 And $2 instead of \1 and \2 for some rather complex reasons. Also look in my signature for an app that will allow you to test SREs and become more familiar with them. George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" 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