我有大量包含坐标和其他信息的特殊字符串。
我需要做的是找到一个以G1
开头并以F4500
结尾的匹配字符串,之后在该匹配字符串中我用{{1}替换G1
}}
示例:
首字母:G0
修改:G1 X95.090 Y104.910 F4500
我现在已经打破了很长一段时间了,似乎无法解决这个问题。
答案 0 :(得分:0)
此模式匹配要替换的字符串:
\bG1(?=.*?F4500\b)
\b // a word boundary
G1 // literal
(?= // look ahead and assert that group matches
.*? // anything
F4500 // literal
\b // a word boundary
) // end of group
在Java中你可以这样做:
"target string here".replaceAll("the regex pattern", "G0");
答案 1 :(得分:0)
sed :
$ echo 'G1 X95.090 Y104.910 F4500' | sed -e 's/G1\(.*F4500\)/G0\1/'
因此,如果您在文件中包含字符串:
$ sed -i -e 's/G1\(.*F4500\)/G0\1/' input.txt