的東西,如這可能工作:
查找模式:
^(.*\] = {1,2})(\$obj->\w+[^;]);
替換模式:
\1isset(\2)?\2:'';
例:
https://regex101.com/r/ZQbAp0/1
^ asserts position at start of a line
1st Capturing Group (.*\] = {1,2})
.* matches any character (except for line terminators)
\] matches the character ] literally (case sensitive)
= matches the characters = literally (case sensitive)
{1,2} Quantifier — Matches between 1 and 2 times, as many times as possible, giving back as needed (greedy)
2nd Capturing Group (\$obj->\w+[^;])
\$ matches the character $ literally (case sensitive)
obj-> matches the characters obj-> literally (case sensitive)
\w+ matches any word character (equal to [a-zA-Z0-9_])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character not present in the list below [^;]
; matches the character ; literally (case sensitive)
真棒。它正在像我想要的那樣工作。但你能再幫我一次嗎?我想搜索'$ array_name ['array_key'];'並用'isset($ array_name ['array_key'])?$ array_name ['array_key']替換它:'';'我嘗試修改正則表達式來實現並仍在嘗試。但是,如果你有答案,那將節省我很多時間。再次感謝:) – siddiq
這應該是相當容易的,雖然'$ array_name'總是與'array_key'一樣的前綴嗎?所以'array'會在兩個地方匹配。基本上,這將找到模式'\ $(。+)(_ name \ [')(\ 1)(_ key'\];)'並替換'isset(\ 1)?\ 1:'';'... –
我真的同意你的答案。但我的數組密鑰是動態的,而且很大。這是將xml元素解析爲數組。所以我的數組元素看起來就像'$ tag_detail ['/ product-biitem/association-ends [1]/association-end [1]/target [1]/reference-to-product-contract [1]/product-contract- key [1]/key-type-for-cbe-standard [1]/object-id [1]']'。我需要爲這種數組元素添加isset。感謝您的回答。這在一些其他情況下幫助了我。如果這是一些容易的事情,那麼這對我很有幫助。 ;):) – siddiq