Jump to content

StringRegExpReplace problem


Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...