-3

sorry about the title, but the stupid thing did not accept anything. I am triyng to do this

Dim myString, myResult
myString = "Hello World! 1234 Hello World! 4321 Hello World! 6789"
Set myRegex = New RegExp
myRegex.Global = true
myRegex.IgnoreCase = true
myRegex.Pattern = "([0-9]+)"
myResult = myRegex.Replace(myString, StrReverse("$1"))

I want all the numbers to reverse I want the string to look like this "Hello World! 4321 Hello World! 1234 Hello World! 9876" but all I get is "1$"

Thanks :-)

dj_boy
  • 103
  • 1
  • 3
  • 9
  • 2
    When your post doesn't get through the filter, you should consider the possibility that it's correct, and improve your post, rather than just bypassing it any way you can. – Blorgbeard Jul 09 '13 at 20:35
  • 2
    "This post does not meet our quality standards" is still correct. :-) Please [edit] your question title to something that is actually applicable to your question, and then edit the text to explain more about what it is you're asking and what's not working in the code you posted. Thanks. – Ken White Jul 09 '13 at 20:35
  • I edited the title, I dont see how I can make my qustion my detailed – dj_boy Jul 09 '13 at 20:38
  • 1
    You're passing `$1` to `StrReverse`, which will return `1$`, and that's what you're getting back. What else would you expect it to do? (`$1` is a string literal the way you're using it, not the matched group in the regular expression that you seem to think it will be; if you explain more clearly what you're trying to do, you'll get help much more quickly. What you've said so far is: "I'm trying to use code that does nothing, and I'm getting exactly the results I asked for with that code.") – Ken White Jul 09 '13 at 20:47
  • if I do not strRevers I get the matched results, But I want to reverse it – dj_boy Jul 09 '13 at 20:50

1 Answers1

2

You can do this using a replacement function:

Function Reverse(m, pos, src)
  Reverse = StrReverse(m)
End Function

Set re = New RegExp
re.Pattern = "\d+"
re.Global = True

s = "Hello World! 1234 Hello World! 4321 Hello World! 6789"

WScript.Echo re.Replace(s, GetRef("Reverse"))

Output:

Hello World! 4321 Hello World! 1234 Hello World! 9876

Just calling re.Replace(s, StrReverse("$1")) won't work, because in this statement, StrReverse reverses the string "$1" before re.Replace() is called.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328