如何使用仅给定索引的正则表达式提取子字符串?
How to extract substring using regex given only the index?
是否有任何方法可以提取 string/sentence 的一部分,仅给出子字符串的起始位置和结束位置的起始和终止索引?
例如:"this is an example00001. and so on." 并且我需要使用正则表达式从位置 10 到 15(即示例)获取子字符串。
使用锚定后看。
以你的位置 10 到 15 为例:
(?<=^.{10}).{5}
如果不支持向后看,请使用第 1 组:
^.{10}(.{5})
我认为您需要从位置 11 获得您想要的匹配。这是一个例子:
$ cat input.txt
This is an example00001. and so on.
$ sed -r 's|(.{10})(.{5})(.*)||' input.txt
exam
$ sed -r 's|(.{11})(.{5})(.*)||' 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
for replacing with the second group
您可能希望在正则表达式中使用 ^ 和 $ 字符作为行首和行尾。
是否有任何方法可以提取 string/sentence 的一部分,仅给出子字符串的起始位置和结束位置的起始和终止索引? 例如:"this is an example00001. and so on." 并且我需要使用正则表达式从位置 10 到 15(即示例)获取子字符串。
使用锚定后看。
以你的位置 10 到 15 为例:
(?<=^.{10}).{5}
如果不支持向后看,请使用第 1 组:
^.{10}(.{5})
我认为您需要从位置 11 获得您想要的匹配。这是一个例子:
$ cat input.txt
This is an example00001. and so on.
$ sed -r 's|(.{10})(.{5})(.*)||' input.txt
exam
$ sed -r 's|(.{11})(.{5})(.*)||' 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
for replacing with the second group
您可能希望在正则表达式中使用 ^ 和 $ 字符作为行首和行尾。