Sign in to follow this  
Followers 0
yoinkster

Help me please with StringRegExpReplace

8 posts in this topic

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.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Something like this:

$string  = StringRegExpReplace($string, "([^\s])-([^\s])","\1 - \2")

Edit: Nevermind I have the wrong idea

Edited by MrMitchell

Share this post


Link to post
Share on other sites

Something like this:

$string  = StringRegExpReplace($string, "([^\s])-([^\s])","\1 - \2")

Edit: Nevermind I have the wrong idea

Think you have right idea

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 by MrMitchell

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by yoinkster

Share this post


Link to post
Share on other sites

#6 ·  Posted (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 by MrMitchell

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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!"

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  
Followers 0