user85
user85

Reputation: 1596

Regex expression for digit followed by dot (.)

I want to find a text with with digit followed by a dot and replace it with the same text (digit with dot) and "xyz" string. For ex.

1. This is a sample
2. test
3. string

**I want to change it to**
1.xyz This is a sample
2.xyz test
3.xyz string

I learnt how to find the matching text (\d.) but the challenge is to find the replace with text. I'm using notepad ++ editor for this, can anyone suggest the "Replace with" string.

Upvotes: 5

Views: 10648

Answers (3)

Terry
Terry

Reputation: 51

For notepad++... You must escape the period/dot character in the expression - precede it with a backslash: \.

In my case, I needed to find all instances of "{EnvironmentName}.api.mycompany.com" (dev.api.mycompany.com, stage.api.mycompany.com, prod.api.mycompany, etc.) I used this search expression:

.*\.api.mycompany.com

Notepad++ RegEx Search Screenshot

Upvotes: 1

Antonio
Antonio

Reputation: 1

I think the right answer is as follow:

Find: ^(\d)([.])(\s) Replace: $1$2XYZ

That will work with "n. " being "n" a digit [0-9]. If the input should accept digits with different lengths like 10, 100, 1000... or multiples dots "." after the digit or multiple spaces after the dot, then the answer is:

Find: ^(\d*)([.])([.]*)(\s*) Replace: $1$2XYZ

Input:


1. This is a sample
2. test
3. string
30. string
10..... string
50005...     string

Output:


1.XYZ This is a sample
2.XYZ test
3.XYZ string
30.XYZ string
10.XYZ string
50005.XYZ string

Upvotes: 0

HamZa
HamZa

Reputation: 14931

First of all, you need to escape the dot since it means "match anything (except newline depending if the s modifier is set)": (\d\.).

Second, you need to add a quantifier in case you have a 2 digit number or more: (\d+\.).

Third, we don't need group 1 in this case: \d+\..

In the replacement, it's quite simple: just use $0xyz. $0 will refer to group 0 which is the whole match.

enter image description here

Upvotes: 8

Related Questions