获取所有方括号内的内容:正则表达式

huangapple go评论60阅读模式
英文:

Get all Content thats between Square Brackets: Regex

问题

"parameters('ECC_Shipment_Trigger_properties_pl_ecc_shipment_adls_sql_parameters_File_system')"

英文:

I have the follwing string:

"[parameters('ECC_Shipment_Trigger_properties_pl_ecc_shipment_adls_sql_parameters_File_system')]"

and I would like to write a regular expression that return only:

parameters('ECC_Shipment_Trigger_properties_pl_ecc_shipment_adls_sql_parameters_File_system')

Keeping in mind that I only need content that starts as:

parameters('some string')

So far I have come up with (?<=[parameters(').+?(?=')) which only returns the text between single quotes.

for testing, I am using https://regexr.com/

How can I modify my current regex to get this result?

答案1

得分: 1

正向后瞻是一种断言(不消耗字符),因此这部分 (?<=\[parameters\(&#39;) 表示从当前位置开始断言左侧是 [parameters(

如果不使用前瞻后顾,可以使用捕获组和一个匹配任何字符但不包括 &#39;否定字符类

值在第一个捕获组中:

[(parameters('[^']+'))]

正则表达式演示

如果想要使用前瞻后顾,这可能是一个选项,断言左侧是 [,右侧是 ]

(?<=[)parameters('[^']+')(?=])

正则表达式演示

英文:

The positive lookbehind is an assertion (non consuming), so this part (?&lt;=\[parameters\(&#39;) means that from the current position it asserts what is on the left is [parameters(.

Instead of a lookaround, you could use a capturing group and a negated character class matching any char except a &#39; instead.

The value is in the first capturing group.

\[(parameters\(&#39;[^&#39;]+&#39;\))\]

Regex demo

If you want to use lookaround, this might be an option, asserting what is on the left is [ and what is on the right is ]

(?&lt;=\[)parameters\(&#39;[^&#39;]+&#39;\)(?=\])

Regex demo

huangapple
  • 本文由 发表于 2020年1月3日 19:03:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定