$1 vs \1

A place to try and solve your RegEx problems.
Post Reply
ebulerdo
Posts: 6
Joined: Mon Dec 09, 2013 9:54 am

$1 vs \1

Post by ebulerdo » Tue Oct 20, 2020 10:19 am

Good morning!

Can you help me understand the difference between $n and \n to refer to the content captured between (), please?

I have an XML file where every record is in a single line, so I search for

Code: Select all

</(.+?)><
and replace it with

Code: Select all

</$1>\r\n<
to move every tag to a new line. It works fine.

Now I realize that some fields in the same file are empty (ex. <MM:section></MM:section>) and want to remove them. So using the same logic as above I search for

Code: Select all

<(.+?)></$1>\r\n
and replace it with nothing. But it doesn't work.

After some testing and researching I realize it does work if I replace $1 with \1. So the issue is resolved. But I don't understand why.

Why did $1 work in the first case but not in the second one? Thank you!
ebulerdo
Posts: 6
Joined: Mon Dec 09, 2013 9:54 am

Re: $1 vs \1

Post by ebulerdo » Tue Oct 20, 2020 10:25 am

Could it be that $1 is not working in the second example because I am using it in the same expression as the captured text? In the first case I used it in the Replace field.
Post Reply