RegEx Replace Question

Hey Gang,

I have the following text:

I want to replace the 1 that is between 38000 and 69 with say a 5. So I want things to look like;

I have the following RegEx Search Pattern Defined:

(sendir,1:[1-3],1,[0-9]+,)([1-5][0-9]?)

It works fine. Captures my possible text values.

Now the replace is where I am having problems. If I do:

$15

That doesn’t work as the RegEx engine is trying to find subexpression 15 which doesn’t exist.

But if I do:

$1B

I get:

I can put any other character in except a number! How do I replace numbers?? I’m obviously doing something very wrong with my replacement pattern.

I’ve just begun to learn to crawl with RegEx and this is the first time I’ve tried to use it as a replacement pattern…

Thanks,

Jon

The traditional “\1” should work, but here’s a simpler solution:

SearchPattern = "(sendir,1:[1-3],1,[0-9]+),([1-5][0-9]?)
ReplacementPattern = "$1,5

“\1” does the same thing. You can’t do " \15."

As usual your suggestion worked. :slight_smile: I didn’t think about moving the comma out of the first group. It was late…

But in general, how do you replace a numeric substring? I can definitely see times when that would be required.

Also, I just did a real check of this in my code and it works. But, it replaces EVERY instance for every match in the string at once. So for example, there are multiple matches of my search pattern. It replaces all the matches.

In this case, I want to replace all the matches, but how do you control the replace so that it only does one at a time?

Set Rg.Options.ReplaceAllMatches = False or use the Replace function in RegExMatch and parse it yourself.

Perfect.

Now, how to do numeric replacements… :slight_smile:

I just tried this and it works perfectly:

  dim rx as new RegEx
  rx.SearchPattern = "(.)(.)"
  rx.ReplacementPattern = "$12"
  
  dim s as string = rx.Replace( "a3" )
  // s = "a2"

I guess I am not getting how replace works. If I have this regex:

(?mi-Us)\\h(Delay)([0-9].+)\\h

And this text:

And I want to make 300, 500, this will not work:

rx.replacementpattern(" $1500 ")

The text becomes:

But if I do:

rx.replacementpattern(" $1A500 ") Then my text is:

So it does indeed matter if there’s a number after the $1 or $2, etc as opposed to a letter…

I’m seeing this all with RegExRX…

Here is what I am doing in a textfield in code to make it work. It works but I’d like to understand better how replace works.

  dim rx as new RegEx
  rx.SearchPattern = "(?mi-Us)\\h(Delay)([0-9].+)\\h"
  rx.ReplacementPattern = " Delay" + me.text + " "
  
  dim rxOptions as RegExOptions = rx.Options
  rxOptions.LineEndType = 4
  rxOptions.ReplaceAllMatches = true

Jon

I hate to hear it but it looks like you found a bug in RegExRX. Try this replace in Xojo and it should work.

OK. I’ll try it out, Kem. Thanks.

I have confirmed that it works great in Xojo. So yeah, bad news and good news.

Bad news: I found a bug.
Good news: I found a bug and your program can be improved upon! :slight_smile: