Replacing *target* characters in RegEx without losing anything

281 Views Asked by At

parameter

In Sublime Text 3 I am using the following RegEx code as my search combo to find any alphabet character preceding 0, followed by a space, followed by another character which is as follows:

RegEx Code

[a-z]0 [a-z]

Search Target Examples

Eg.1  a0 p
Eg.2  p0 sa
Eg.3  ap0 l

Question: What RegEx code should I write so that it replaces all such occurrences containing the 0s with a dot (.) i.e., it should become like this:
Replace Objective of Target Examples

Eg.1  a0 p    → a. p
Eg.2  p0 s    → p. sa
Eg.3  ap0 l   → ap. l
1

There are 1 best solutions below

4
Wiktor Stribiżew On BEST ANSWER

Use capturing groups and backreferences:

Regex: ([a-z])0( [a-z])
Replacement: $1.$2

See the regex demo

Details:

  • ([a-z]) - matches and captures into Group 1 any ASCII lowercase letter
  • 0 - a literal 0 is matched
  • ( [a-z]) - Group 2: a space and a lowercase ASCII letter

The replacement contains the backreference to the value stored in Group 1 ($1), then we replace 0 with a dot, and use the backreference to Group 2 contents ($2).

enter image description here