Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

regex - Regular expressions: how to find any word with the form [(non quotation mark)whatever(non quotation mark)]

I want to find any word where it has a square bracke, then no simple quotation mark, then whatever, then no simple quotation mark and then the closed square bracke.

For example, look this words:

['car in the mountain']
[sister in law]
['dog']

The result must show me:

[sister in law]

Exists any regular expression can do it that?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You may use this regex with a negative lookahead:

[(?!'[^]]*'])[^]]*]

RegEx Demo

RegEx Details:

  • [: Match starting [
  • (?!'[^]]*']): Negative lookahead to fail the match when we have a ' next to [ and just before ]
  • [^]]*: Match 0 or more characters that are not ]
  • ]: Match closing ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...