Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 685
user85 Avatar asked May 23 '14 09:05

user85


People also ask

How do you indicate a dot in regex?

in regex is a metacharacter, it is used to match any character. To match a literal dot in a raw Python string ( r"" or r'' ), you need to escape it, so r"\." Unless the regular expression is stored inside a regular python string, in which case you need to use a double \ ( \\ ) instead.

What does a period mean in regular expressions?

The period (.) represents the wildcard character. Any character (except for the newline character) will be matched by a period in a regular expression; when you literally want a period in a regular expression you need to precede it with a backslash.

What is the regex for comma?

The 0-9 indicates characters 0 through 9, the comma , indicates comma, and the semicolon indicates a ; . The closing ] indicates the end of the character set. The plus + indicates that one or more of the "previous item" must be present.

How do you match a space in regex?

If you're looking for a space, that would be " " (one space). If you're looking for one or more, it's " *" (that's two spaces and an asterisk) or " +" (one space and a plus).


2 Answers

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

like image 119
2 revs Avatar answered Oct 24 '22 05:10

2 revs


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

like image 42
Terry Avatar answered Oct 24 '22 04:10

Terry