Jump to content
Sign in to follow this  
Antiec

StringRegExpReplace problem

Recommended Posts

Antiec

Hi!

I'm trying to replace some text in a string and I managed to make it work, but it's a bit weird.

Why does the following function replace "test name=" too? Should it? How to format the regexp so it won't replace it? $1 has "test_5" so shouldn't it replace only that part?

$name = "dummy_"
for $i = 0 to 10
  $strtest = StringRegExpReplace( 'test name="dummy_5"', 'test name="(.*)"',     $name & $i  )
  MsgBox( 0, "Test", $strtest )
Next

Ofcourse $strtest = StringRegExpReplace( 'test name="dummy_5"', 'test name="(.*)"', 'test name="' & $name & $i & '"' ) works but isn't as neat =/

Sorry for the typos

Share this post


Link to post
Share on other sites
weaponx

Maybe this?

$string = 'test name="dummy_5"'
$name = "dummy_"
for $i = 0 to 10
  $strtest = StringRegExpReplace($string, '(test name=")(.*)(")',  "$1" & $name & $i & "$3"  )
  ConsoleWrite($strtest & @CRLF)
Next

Share this post


Link to post
Share on other sites
Antiec

Thx, it does make a bit neater and taught me a bit about regexp syntax. I had a second problem too, but managed to fix it. I'd just like to know is it normal too and why. It's kinda weird...

If I use this one, it will result in 'to port=" />'

MsgBox( 0, "Test", StringRegExpReplace( '< to port="500" />', '(to port=)(.*)(" />)', "$1" & 6000 & "$3" ) )

But if I add the " quotation it does work. It seems to be the same always if I want to replace with numbers.

MsgBox( 0, "Test", StringRegExpReplace( '< to port="500" />', '(to port=)(.*)(" />)', "$1" & '"' & 6000 & "$3" ) )

Share this post


Link to post
Share on other sites
weaponx

MsgBox( 0, "Test", StringRegExpReplace( '< to port="500" />', '(")(.*)(")', '"' & 6000 & '"') )

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.