通过使用正则表达式,我想获得出现以下字符序列的所有索引:
FORALL ... in ... :
//“FORALL”和“in”之间可能是空格和非单词字符,如“,”
例如: 找到这3个:
替换为这个:
FORALL i,j in a1:
部分想法:
子字符串必须以FORALL字开头
“in”和“:”必须恰好出现一次 - 因此不要错误地从实际的下一次独立比赛中得到“in”
子串内部不允许“in”,但第一部分应以一个
我的最后一次尝试是以下正则表达式
"(^FORALL[<=|>=|<|>|==|!=](?!.*in).*in$)((?!.*\\:).*\\:$ )"
答案 0 :(得分:2)
你可以尝试一下这个正则表达式吗? (它是一个文字字符串)
^(FORALL [^<=>,: ]+) *[<=>,]+ *([^<=>,: ]+)\s+in\s+([^,:]+)[^:]*:$
这是一个细分:
^ # Beginning of string
( # 1st capture begins
FORALL # Match FORALL and a space
[^<=>,: ]+ # Any characters except <=>,: or space
) # 1st capture ends
* # Any spaces
[<=>,]+ * # Any characters of <=>, followed by any spaces
( # 2nd capture begins
[^<=>,: ]+ # Any characters except <=>,: or space
) # 2nd capture ends
+in + # Match in surrounded by spaces
([^,:]+) # Match any non , or : characters
[^:]*: # Match any non : characters, then match a :
$ # End of string
并替换为:
$1,$2 in $3: