简体   繁体   中英

Regex condition after and before a known phrase

I am trying to capture a phrase that starts with a Capital letter between 2 known phrase. Let's say between "Known phrase, " and the word "The".

For example in the text below, the phrase I'm trying to capture is: Stuff TO CApture That always start with Capital letter but stop capturing when

Ignore Words Known phrase, ignore random phrase Stuff TO CApture That always start with Capital letter but stop capturing when The appears.

Regex I have tried: (?<=Known phrase, ).*(?= The) and Known phrase, (.*) The These regex also captures ignore random phrase . How do I ignore this?

I guess as regular expression is left side greedy you should first try to match anything that is not capital letters

Something like /Start[^AZ]*(.*)stop/ ( [^AZ] matches anything that is not capital letter)

regex101 demo

For your exaple data, you might use:

Known phrase, [az ]+([AZ].*?) The

See the regex demo

Explanation

  • Known phrase, Match literally
  • [az ]+ Match 1+ times a lowercase character or a space (Add to the character class what you would allow to match except an Uppercase character)
  • ([AZ].*?) Capture in a group matching an uppercase character followed by 0+ times any character except a newline.
  • The Match literally

I'm not sure of what you are trying to do, but, trying to stick with your code, (?<=Known phrase, )([^AZ]*)(.*)(?=The) should do the trick: the text you need is in the group 2.
If you need to match everything just change to (.*)(?<=Known phrase, )([^AZ]*)(.*)(?=The)(.*) and get your text in group 3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM