RegeEx Group Capture Syntax for Notepad ++

I have a list of label names in a text file that I would like to use with Find and Replace in Notepad ++, they are listed as follows:

MyLabel_01 MyLabel_02 MyLabel_03 MyLabel_04 MyLabel_05 MyLabel_06 

I want to rename them in Notepad ++ as follows:

 Label_A_One Label_A_Two Label_A_Three Label_B_One Label_B_Two Label_B_Three 

The regex that I use in the Notepad ++ replacement dialog box to commit the label name is the following:

 ((MyLabel_0)((1)|(2)|(3)|(4)|(5)|(6))) 

I want to replace each capture group as follows:

 \1 = Label_ \2 = A_One \3 = A_Two \4 = A_Three \5 = B_One \6 = B_Two \7 = B_Three 

My problem is that Notepad ++ does not register the regular expression syntax above. When I click Count in the Replace dialog box, it returns with 0 occurrences. Not sure what is going on in the syntax. And yes, I made sure the Regular Expression radio button is selected. Help is appreciated.

UPDATE:

Tried to slip away from the bracket, still not working:

 \(\(MyLabel_0\)\((1\)|\(2\)|\(3\)|\(4\)|\(5\)|\(6\)\)\) 
+4
source share
4 answers

Ed's response showed a working pattern, since striping is not supported in Notepad ++, however the rest of your problem cannot be handled only with regular expression. What you are trying to do is not possible with the search / replace regular expression approach. Your desired result includes logical conditions that cannot be expressed in regular expression. All you can do with the replacement method is to reorder the elements and refer to the captured elements, but you cannot tell them to use β€œA” for values ​​1-3 and β€œB” for 4-6. In addition, you cannot designate such placeholders. They are really capture groups that you are re-contacting.

To achieve the results that you showed, you will need to write a small program that will allow you to check the values ​​obtained and make the appropriate replacements.

EDIT: here is an example of how to achieve this in C #

 var numToWordMap = new Dictionary<int, string>(); numToWordMap[1] = "A_One"; numToWordMap[2] = "A_Two"; numToWordMap[3] = "A_Three"; numToWordMap[4] = "B_One"; numToWordMap[5] = "B_Two"; numToWordMap[6] = "B_Three"; string pattern = @"\bMyLabel_(\d+)\b"; string filePath = @"C:\temp.txt"; string[] contents = File.ReadAllLines(filePath); for (int i = 0; i < contents.Length; i++) { contents[i] = Regex.Replace(contents[i], pattern, m => { int num = int.Parse(m.Groups[1].Value); if (numToWordMap.ContainsKey(num)) { return "Label_" + numToWordMap[num]; } // key not found, use original value return m.Value; }); } File.WriteAllLines(filePath, contents); 

You should be able to use it easily. Perhaps you can download LINQPad or Visual C # Express to do this.

If your files are too large, this may be an inefficient approach, in which case you can use StreamReader and StreamWriter to read from the source file and write it to another, respectively.

Also remember that my sample code writes back to the source file. For testing purposes, you can change this path to another file so that it is not overwritten.

+5
source

Bar bar bar - Notepad ++ thinks you're a barbarian.

(deprecated - see update below.) No vertical bars in Notepad ++ regex - sorry. I also forget every few months!

Use [123456] instead.

Update : Sorry, I did not read carefully enough; in addition to the bug with the bugging, @Ahmad spot-on - you cannot make such a replacement.

Update . Version 6 of Notepad ++ changed the regex engine to Perl-compatible, which supports "|". AFAICT, if you have version 5., automatic updating will not be updated to 6. - you need to explicitly download it.

+4
source

Find and replace regular expressions for

 MyLabel_((01)|(02)|(03)|(04)|(05)|(06)) 

with

 Label_(?2A_One)(?3A_Two)(?4A_Three)(?5B_One)(?6B_Two)(?7B_Three) 

working on Notepad 6.3.2

The outermost pair of brackets is for grouping, they limit the area of ​​the first rotation; not sure if they can be omitted, but they also make the area visible. The pattern searches for a fixed string, followed by one of the two-digit pairs. (The leading zero can be taken into account and put in a fixed line.) Each pair of digits is wrapped in parentheses so that it is captured.

In the replacement expression, the sentence (?4A_Three) says that if capture group 4 matches something, insert the text A_Three , otherwise insert nothing. Similarly for other items. Since 6 alternatives are mutually exclusive, only one will match. Thus, only one of the sentences (?...) will match, and therefore only one will insert text.

0
source

The easiest way to do this, which I would recommend, is to use AWK. If you are on Windows, look for the precompiled mingw32 binaries for it (it will be called gawk).

 BEGIN { FS = "_0"; a[1]="A_One"; a[2]="A_Two"; a[3]="A_Three"; a[4]="B_One"; a[5]="B_Two"; a[6]="B_Three"; } { printf("Label_%s\n", a[$2]); } 

Run on Windows as follows:

 C:\Users\Mydir>gawk -f test.awk awk.in Label_A_One Label_A_Two Label_A_Three Label_B_One Label_B_Two Label_B_Three 
-2
source

Source: https://habr.com/ru/post/1382078/


All Articles