当前位置:网站首页>ICER skill 01 regular matching

ICER skill 01 regular matching

2022-06-23 04:57:00 Eight four one one

1. Match instructions

Symbol effect
. Matches any character
[abc] Match any one of the characters in the formula brackets ( have access to - Indicates the character range , Such as [a-z 0-9] Match lowercase letters and Arabic numerals )
[^abc] Matches any character except those in square brackets
\d Match Arabic numerals , Equate to [0-9]
\D Match any character other than Arabic numerals , Equate to [^0-9]
\x Match hexadecimal numbers
\X Matches any character other than a hexadecimal digit
\w Match word letters , Equate to [0-9 A-Z a-z]
\W Match any character other than the letter of the word
\t matching TAB character
\s Matching blank character
\S Match non white space characters

2. Position symbol

Symbol The position of the representation
$ Match the end of the line
^ Match the beginning of the line ( and [^abc] The symbols in have different functions )
\ < Match the beginning of the word
\ > Match word endings

3. Quantity symbol

Symbol Quantity represented
* matching 0- Any one
\ + matching 1- Any one
\ ? matching 0-1 individual
\ {n,m} matching n-m individual
\ {n} matching n individual
\ {n,} matching n- Any one
\ {,m} matching 0-m individual

4. Special symbols

Symbol effect
\ * matching * character
\ . matching . character
\ / matching / character
\ \ matching \ character
\ [ matching [ character

5. A few examples

① Put all the words help or Help All in capital letters HELP
:%s/[Hh]elp/HELP/g

  • : Command instructions
  • % All lines are valid
  • s Search and replace
  • [Hh] belt H or h

② Replace one or more spaces with a single space
:%s/ \ +//g

  • \ + It means quantity
  • // No space in is no space

③ Delete all blank lines ( There are no characters , Except for a carriage return )

  • :g/^ $ /d: If there is no character between the beginning and end of the line, it will be deleted
  • :g/[ \t]*$/d: Multiple spaces are deleted at the end of the line

④ Delete lines containing certain characters
:g/ERROR/d

  • It is good to remember hhh

⑤ Delete all leading spaces in each line
:%s/^ *//g

  • ^ *: Indicates that the character is known from the beginning of the line

⑥ Insert... At the beginning of each line in the file “->”
:%s/^/->/g

  • Positioning line head , Insert a character

⑦ Delete with [a,b,c] Beginning line
:%s/^ [a|b|c].*$//g

  • Delete abc Beginning line , instead of a The beginning or b Opening or c start

⑧ Use “\ (” and “\ )” The symbol encloses the normal expression , You can use it later \1 \2 And other variables \ ( and \ ) The content in

example :/ \ (a\ +\ )[^a]\ +\1
At the beginning and end a The same number of strings

  • It is equivalent to saving and calling

matching aabbbaa
matching aaacccaaa
Mismatch abbbaa

⑧ Swap the order of all hyphenated entries in the list
ABC: XYZ
: %s/ (.* \ ):\ (.*\ )/\2:\ 1/g

  • Save any character to \1
  • Then match the colon :
  • Save any characters to \2
  • then 1 2 Swap for
原网站

版权声明
本文为[Eight four one one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230110550584.html