问题描述
我有一堆文件的句子结尾是这样的: \ @.下一个句子
.我想在句点之后插入一个空格.
I've got a bunch of files that have sentences ending like this: \@.Next sentence
. I'd like to insert a space after the period.
并非所有出现的 \ @.
都没有空格,因此我的正则表达式检查句点后的下一个字符是否为大写字母.
Not all occurrences of \@.
do not have a space, however, so my regex checks if the next character after the period is a capital letter.
因为我要在句点之后检查一个字符,所以我不能仅将 \ @.
替换为 \ @.
,并且因为我没有不知道接下来的角色是什么,我被困住了.
Because I'm checking one character after the period, I can't just do a replace on \@.
to \@.
, and because I don't know what character is following the period, I'm stuck.
我当前的命令:
sed -i .bak -E 's/\\@\.[A-Z]/<SOMETHING IN HERE>/g' *.tex
如何获取匹配字符串的最后一个字母以用于替换正则表达式?
How can I grab the last letter of the matching string to use in the replacement regex?
为了记录,我使用的是 sed
的BSD版本(我使用的是OS X)-来自我的(显然是BSD sed)(或至少是Apple)的先前问题版本)并不总是与GNU sed正则表达式配合使用.
For the record, I'm using a BSD version of sed
(I'm using OS X) - from my previous question regarding sed
, apparently BSD sed (or at least, the Apple version) doesn't always play nice with GNU sed regular expressions.
推荐答案
正确的命令应为:
sed -i.bak -E "s/\\\@.(\S)/\\\@. \1/g" *.tex
在此情况下,您匹配任何 \ @
,后跟非空格( \ S
),然后插入空格(将整个匹配项替换为'\ @'
加上刚刚找到的非空格.
Whith it, you match any \@
followed by non whitespace (\S
) and insert a whitespace (what is made by replacing the whole match with '\@ '
plus the the non whitespace just found).
这篇关于使用sed在句点后插入空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!