我一直在努力理解正则表达式,有没有办法可以替换两个字符串之间的字符/例如我有
sometextREPLACEsomeothertext
我想替换,REPLACE(可以是实际工作中的任何东西)只在sometext和someothertext之间与其他字符串.任何人都可以帮我这个.
编辑 假设,我的输入字符串是
sometext_REPLACE_someotherText_something_REPLACE_nothing
我想在sometext和someotherText之间替换REPLACE文本,从而产生以下输出
sometext_THISISREPLACED_someotherText_something_REPLACE_nothing
谢谢
如果我正确理解你的问题,你可能想要使用lookahead和lookbehind来表达你的正则表达式
(?<=...) # matches a positive look behind
(?=...) # matches a positive look ahead
Run Code Online (Sandbox Code Playgroud)
从而
(?<=sometext)(\w+?)(?=someothertext)
Run Code Online (Sandbox Code Playgroud)
将任何'单词'与'sometext'后面的至少1个字符匹配,然后跟'someothertext'
在C#中:
result = Regex.Replace(subject, @"(?<=sometext)(\w+?)(?=someothertext)", "REPLACE");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5404 次 |
| 最近记录: |