我有一根线,看起来像这样:oldString="this is my {{string-d}}" => "this is my {{(string-d)}}"
oldString2="this is my second{{ new_string-d }}" => "this is my second{{ (new_string-d) }}"
oldString2="this is my second new_string-d " => "this is my second (new_string-d) "
oldString2="this is my second new[123string]-d " => "this is my second (new[123string]-d) "
每当我看到“-d”在它后面和它所附加的单词之前,我都想加上括号。在
我写了一段代码,在字符串中寻找模式“-d”,并在找到模式后将字符串分区到“-d”之前、“-d”和“-d”后面的3个分区,然后检查“-d”之前的块,直到找到空格或“{”,然后停止并添加括号。我的代码如下:
P、 我有很多文件,我从他们那里读,并试图修改字符串,上面的例子只是为了演示我在做什么。在
^{pr2}$
运行代码的结果是:newString = "(this is my {{string-d )"
如你所见,开始的括号在“this”之前,而不是“string”之前,为什么会这样?另外,我不确定这是不是最好的方法来做这种寻找和替换任何建议将不胜感激。在