Skip to main content
Added the adder (-;
Source Link
Philippos
  • 2.7k
  • 1
  • 12
  • 39

Use back references

Most people know that you can mark some parts of your regex with \(\) and later refer to it as \1 (or \2 for the second and so on) in your substitution.

But you hardly ever see back references, using \1 in the regex itself. For extended regular expressions (option -E to sed), this has been removed from the standard, but GNU sed supports it anyhow, so it works at tio.run, for example.

For example, this ERE (.)\1 matches a double character or this one (.).*\1 appearing twice in the pattern space. See here for an example, how this makes things easier (and much shorter!).

Complicated task like this one looking for matches in a file or this one or this one would almost be impossible without this feature.

Math with back references

And it's great for teaching sed to count. I know you can solve some of those problems using y, but as soon as you have more than one thing to increment or decrement, this is the way to go. See examples here or here or try the 151-byte decimal add online to examine how it works.

Use back references

Most people know that you can mark some parts of your regex with \(\) and later refer to it as \1 (or \2 for the second and so on) in your substitution.

But you hardly ever see back references, using \1 in the regex itself. For extended regular expressions (option -E to sed), this has been removed from the standard, but GNU sed supports it anyhow, so it works at tio.run, for example.

For example, this ERE (.)\1 matches a double character or this one (.).*\1 appearing twice in the pattern space. See here for an example, how this makes things easier (and much shorter!).

Complicated task like this one looking for matches in a file or this one or this one would almost be impossible without this feature.

And it's great for teaching sed to count. I know you can solve some of those problems using y, but as soon as you have more than one thing to increment or decrement, this is the way to go. See examples here or here.

Use back references

Most people know that you can mark some parts of your regex with \(\) and later refer to it as \1 (or \2 for the second and so on) in your substitution.

But you hardly ever see back references, using \1 in the regex itself. For extended regular expressions (option -E to sed), this has been removed from the standard, but GNU sed supports it anyhow, so it works at tio.run, for example.

For example, this ERE (.)\1 matches a double character or this one (.).*\1 appearing twice in the pattern space. See here for an example, how this makes things easier (and much shorter!).

Complicated task like this one looking for matches in a file or this one or this one would almost be impossible without this feature.

Math with back references

And it's great for teaching sed to count. I know you can solve some of those problems using y, but as soon as you have more than one thing to increment or decrement, this is the way to go. See examples here or here or try the 151-byte decimal add online to examine how it works.

Source Link
Philippos
  • 2.7k
  • 1
  • 12
  • 39

Use back references

Most people know that you can mark some parts of your regex with \(\) and later refer to it as \1 (or \2 for the second and so on) in your substitution.

But you hardly ever see back references, using \1 in the regex itself. For extended regular expressions (option -E to sed), this has been removed from the standard, but GNU sed supports it anyhow, so it works at tio.run, for example.

For example, this ERE (.)\1 matches a double character or this one (.).*\1 appearing twice in the pattern space. See here for an example, how this makes things easier (and much shorter!).

Complicated task like this one looking for matches in a file or this one or this one would almost be impossible without this feature.

And it's great for teaching sed to count. I know you can solve some of those problems using y, but as soon as you have more than one thing to increment or decrement, this is the way to go. See examples here or here.