首页 > 其他分享 >Understanding RegEx with Notepad++ 正则表达式

Understanding RegEx with Notepad++ 正则表达式

时间:2023-11-06 12:00:43浏览次数:38  
标签:RegEx used ++ Notepad example matches characters Find match

Searching a string using the ‘Find‘ or ‘Find & Replace‘ function in text editors highlights the relevant match (e.g. searching ‘le‘ highlights it inside words such as ‘apple‘, ‘please’ etc). However, some advanced editors such as Notepad++ (I mention Notepad++ in my examples since its my favourite so far!) supports the use of regex, which recently saved me hours of manually replacing strings and numeric values in files containing HTML and JacaScript codes.

Regex characters can be used to create advanced matching criteria. The following table introduces some of them with practical examples. But before starting make sure that you change the Search Mode from Normal to Regular expression in your Find or Find & Replace dialogue box.

  • [ ]

The square brackets can be used to match ONE of multiple characters. For instance, [abc] matches any of the characters a, b or c. Hence, b[eo]n will match words like ben and bon, but not been or beon. Ranges can also be used, [a-z] is any lower case character and so on.

  • ^

The caret can be used inside the square brackets to exclude characters from the match. For instance, hell[^o] means the string ‘hell’ will be ignored if followed by the letter ‘o’. Another example is [^A-Za-z] which will exclude all alphabetic characters.
However, if not placed inside a set, ^ can be used to matches the start of a line.

  • $

This matches the end of a line.

  • .

The period or dot matches any character.

  • \d

Matches any single digit.

  • \w

Matches any single alphanumeric characters or underscore.

  • \s

Matches whitespaces including tabs and line breaks.

  • *

The asterisk or star sign matches 0 or more times. For example, Ba*m matches Bm , Bam , Baam etc.

  • +

The plus sign matches 1 or more times. For example, lo+l matches lol , lool , loool etc.

  • \<

Matches the start of a word. For example, \< directly followed by 'sh' matches 'she' but does not matches 'wish'.

  • \>

Matches the end of a word. For example, sh\> matches ‘wish’ and does not matches ‘she’.

  • ( )

The round brackets can be used in the Find & Replace function to tag a match. tagged matches can then be used in replace with \1, \2 etc.

For example, If you write 123xxxRRR in the search and 123\1HHH in the ‘Replace with’ filed, the result will be: 123xxxHHH.

  • \

The backslash can be used to escape regex characters. For example to match 1+1=2, the correct regex is 1\+1=2. Otherwise, the plus sign will have a special meaning.

Further, the following two examples should be giving you a better idea of how to use regex in your editor:

  • Find: Win([0-9]+) Replace with: Windows\1

Will search for strings like Win2000, Win2003 and changes them to Windows2000, Windows2003…

  • Find: [a-z]+(\d\d)\> Replace with: Windows\1

Will search for all alphanumerics followed by 2 digits only at the end such as Win98 and Win07 and changes them to Windows98, Windows07…

 

http://blog.hakzone.info/posts-and-articles/editors/understanding-regex-with-notepad/comment-page-1/

 



标签:RegEx,used,++,Notepad,example,matches,characters,Find,match
From: https://blog.51cto.com/emanlee/8203600

相关文章

  • C++使用冒泡排序算法对数组进行排序
     #include<iostream>//包含iostream库usingnamespacestd;//使用标准命名空间intmain(){//主函数intarr[]={5,3,2,8,6,7,1,4};//定义并初始化数组intn=sizeof(arr)/sizeof(arr[0]);//计算数组长度//使用冒泡排序算法对数组进......
  • 力扣905 按奇偶排序数组 C++ 双指针+一次遍历
    905.按奇偶排序数组classSolution{public:vector<int>sortArrayByParity(vector<int>&nums){inti=0,j=nums.size()-1;while(i<nums.size()-1&&i<j){while(i<j&&(nums[i]%2==0))i++;......
  • C++_18_多态 - 重写版
     多态:面向对象三大概念:封装、继承、多态!可想而知多态是何等的重要多态的概念以及前提条件:编译期绑定(静态联编):函数入口地址和函数名在编译期间绑定,即编译期间确定函数名和入口地址唯一对应运行期绑定(动态联编):函数入口地址和函数名在编译期间不绑定......
  • C++_17_多继承和虚基类 - 重写版
    多继承单继承:一个派生类只有一个基类,这就是单基类继承,简称“单继承”多继承:一个派生类允许有两个及以上的基类,这就是多基类继承,简称“多继承”单继承中,派生类是对基类的特例化,例如编程类书籍是书籍中的特例。而多继承中,派生类是所有基类的一种组合。在多继承中,派......
  • C++_15_友元函数和友元类 - 重写版
    友元函数和友元类友元函数:通过friend关键字,将不属于当前类的函数在当前类中加以声明,使其成为友元函数,同时该函数能够访问private属性的成员变量。友元类:有有元函数,自然也能有友元类,通过friend关键字,将类A在类B中声明,那么类A会成为类B的友元类注意:1、友......
  • C++_14_常量指针—this指针 - 重写版
    常量指针—this指针this指针:成员函数一般都会拥有一个常量指针(this),指向调用函数的对象,储存的是改对象的首地址(注意:静态成员函数是没有this指针的)//标准写法classbook{public:book(){this->price=0.0;this->title=NULL;}private:doubleprice;char......
  • C++_22_string类型 - 重写版
    string类型·变量定义C++中提供了一个string内建数据类型,它可以替代C语言中的char*数组。使用string数据类型时,需要在程序中包含头文件<string>#include<iostream>#include<string>usingnamespacestd;intmain(){strings1;//......
  • C++_21_重载、重写、重定义 - 重写版
    1、重载同一作用域的同名函数,重复定义;参数格式、参数顺序或者参数类型不同;函数重载和函数的返回值没有任何关系;(const类型的重载本质上是参数类型不同) 2、重写(覆盖)有继承关系子类(派生类)重写父类(基类)的虚函数函数的返回值,函数名字,函数参数,必须和基类中的虚函数一致,主要就是覆盖......
  • C++_20_操作符重载和函数重载 - 重写版
    操作符(运算符)重载 操作符重载指的是将C++提供的操作符进行重新定义或者多重定义,使之满足我们所需要的一些功能。在C++中,经常有需要对多个对象进行算术运算,但是对象比不是基本的数据类型,所以这些运算符都无法执行。为了让程序识别这些运算符,就需要对运算符进行重载......
  • C++_19_虚函数、纯虚函数和抽象类 - 重写版
    虚(成员)函数:在C++中,只有类中的成员函数能被声明为虚函数,顶层函数则不能被声明为虚函数;并且如果在类内声明类外定义,则只在类内声明时加virtual声明虚函数是为了构成多态,多态需要继承关系,需要在类中声明;虚函数能被继承:基类被声明为虚函数,那么派生类即便未添......