有没有一种方法可以仅根据子字符串的起始和结束位置的索引提取字符串/句子的一部分呢? 例如:"this is an example00001. and so on.",我需要使用正则表达式从位置10到15获取子字符串(即,examp)。
有没有一种方法可以仅根据子字符串的起始和结束位置的索引提取字符串/句子的一部分呢? 例如:"this is an example00001. and so on.",我需要使用正则表达式从位置10到15获取子字符串(即,examp)。
使用向后查找并锚定到开头。
以位置10至15为例:
(?<=^.{10}).{5}
^.{10}(.{5})
ID
,停在结尾的第17个字符,还是其他什么地方?(但尝试使用 ^.*(?=ID)
) - Bohemian我认为您需要从位置11开始获取所需的匹配项。以下是一个示例:
$ cat input.txt
This is an example00001. and so on.
$ sed -r 's|(.{10})(.{5})(.*)|\2|' input.txt
exam
$ sed -r 's|(.{11})(.{5})(.*)|\2|' input.txt
examp
-r extended regular expressions (only on gnu sed)
s for substitution
| for separator
(.{11}) for the first group of any 11 characters (you might want 10)
(.{5}) for the second group of any 5 characters
(.*) for any other character, not really needed though
\2 for replacing with the second group