我一直在学习BBEdit手册中的Regex/grep,除了这个部分外,它一直很流畅(它接近尾声,而不像以前的章节不提供解释)。我有任何人都可以解释这个正则表达式模式?
两个大的部分困难是[^]部分,第一部分^(。*)
首先,是说什么,但一个空间?那么它是如何抓住X. Potter,在X之后留出空间?其次,手册讨论了非贪婪修饰符,这些修饰符默认情况下不会匹配最长的模式,并且意外地匹配您的整个短语。 ^(。*)如何不匹配整行并使其成为\ 1?行的开始,零次或多次除了回车之外的任何事情?那怎么能抓住Junior X. Potter作为一种模式?我认为我们必须在这里使用一个非贪婪的限定词,但它似乎不是。
最后,空间在正则表达式模式中究竟做了什么?他们代表自己,(我认为你不需要那么做吗?)或者一个简单的空间(不需要做\ space来逃脱它)。
Rearranging Name Lists
You can use grep patterns to transform a list of names in first name first form to last name first order (for a later sorting, for instance). Assume that the names are in the form:
Junior X. Potter
Jill Safai
Dylan Schuyler Goode
Walter Wang
If you use this search pattern:
^(.*) ([^ ]+)$
And this replacement string:
\2, \1
The transformed list becomes:
Potter, Junior X.
Safai, Jill
Goode, Dylan Schuyler
Wang, Walter
啊,这很有道理,谢谢。 – 2011-12-27 21:14:33