background

SublimeText is a text editor on mac, similar to notepad++ or editplus on windows.

Regular expressions are regular expressions for finding, replacing, etc. text. Details: [http://www.runoob.com/regexp/regexp-intro.html] (http://www.runoob.com/ Regexp/regexp-intro.html)

Received a small demand today, you need to update the country, province, city and other values ​​in the left word document in the figure to the right format:

The demand is not that hard, but I feel that writing a separate program for this conversion is a bit of a killer!

Then, just think of a way to get it directly in the text editor!

Demonstration

The gif image may be a bit big, please be patient. .

Decomposition description

Those who are familiar with regular expressions should understand it after reading it. Can you take it directly?

Matching regular: ([^\x00-\xff]+)\n([a-zA-Z]+)
Replace with: country.$2=$1\n (the end of the \n can be used here)

Match blank lines: ^(\s*)\n
Replace with: (empty, nothing is entered)

Attach some of the original string (copying from the word document becomes the following), it is convenient to test:

不丹
bt
东帝汶
tl
中国
cn
中非
cf
丹麦
dk
乌克兰
ua
乌兹别克斯坦
uz
乌干达
ug
乌拉圭
uy
乍得
td

Visitors who are not familiar with SublimeText and regulars should look down: (Please be sure to look at the pictures!)

  1. sublime text editor operation

   

Note that after opening the replacement action bar, remember to mark the place below the arrow (this means that the regular mode is enabled):

The Find What column is the regularity of the string to match, and the Replace With column is the regular to replace "Yes".

Regular disassembly instructions:

Matching regular: ([^\x00-\xff]+)\n([a-zA-Z]+)
Replace with: country.$2=$1
  1. Match a single Chinese character

  1. Match multiple Chinese characters (more than one "+" in the first step, meaning that one or more of the left characters next to "+" can appear)

  1. Match newline characters (\n on linux/mac and \r\n on windows)

Keep an eye out for the "frame" changes around the characters in the editor!

  1. Match a single English character (only the lowercase letters of a-z are actually here)

  1. Match multiple English characters (same reason, add "+" at the end)

  1. Grouping is easy to refer to (not grouped for grouping, it can be referenced when it is convenient to replace)

  1. Reference grouping, complete replacement

Last modified: 2019年3月29日

Author

Comments

Write a Reply or Comment

Your email address will not be published.