Regex match everything after conditional match

VincFort

am looking to match everything before (and after) zero or more from a list of items. Here is what I am currently using: (?<=\))(.*)(?=(or|,|\())

In this case, I want to match everything after a closing parenthesis ) and everything before or , or ). This works ok (probably not optimally), however, if there are none of the 3 items match, there are no matches.

For example, the sentence 2 cups (500 ml) mushroom, or vegetable broth matches for mushroom, however, 2 cups (500 ml) mushroom doesn't match anything.

Basically, my goal is to find the ingredient from the ingredient + qty string, with the above sentence matching mushroom and the sentence salt matching the whole string salt

Here are more examples:
1 thyme sprig should match thyme sprig
1 garlic clove, chopped should match garlic clove
1 cup (180 g) quinoa, rinsed and drained should match quinoa
2 tbsp (30 ml) olive oil, plus more for serving should match olive oil
Vegan Parmesan, to taste returns Vegan Parmesan

The difference between the first 2 and last 2 is tricky, as if there is a closing parenthesis (as in the last 2 examples), the ingredient should be after the closing parenthesis. If there are no closing parenthesis (as in the first 2 examples, everything after the number should be taken.

Wiktor Stribiżew

You can use

(?:^[^)\n]*\)|\d)\s*\K.*?(?=or|[,(]|$)

If your regex flavor does not support \K, use a capturing group:

(?:^[^)\n]*\)|\d)\s*(.*?)(?=or|[,(]|$)

See the regex demo. Get Group 1 value.

Details:

  • (?:^[^)\n]*\)|\d) - either start of string and then zero or more chars other than a newline or ) char and then a ) char, or a digit (if there is no ) on the line)
  • \s* - zero or more whitespaces
  • (.*?) - Group 1: any 0+ chars other than line break chars, as few as possible
  • (?=or|[,(]|$) - up to the first occurrence of or, ,, (, or end of string.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

RegEx: Match everything but X, then X

분류에서Dev

Regex to match conditional IP address

분류에서Dev

Regex that match everything except the list of strings

분류에서Dev

Regex: Match everything, but a new empty line

분류에서Dev

Match and remove everything up to and including "=" in string with regex

분류에서Dev

Regex to match everything in a string except "<br>", "<br />", "<br />"

분류에서Dev

How to extract specific line after regex match

분류에서Dev

regex to match first n lines after a specific word in Google Spreadsheet

분류에서Dev

Regex to add text before and after the match. vb.net

분류에서Dev

regex (?!) and (?=) and null match

분류에서Dev

Regex multiline match

분류에서Dev

Match regex with \\n in it

분류에서Dev

Regex match tag with no spaces

분류에서Dev

Regex match for split

분류에서Dev

Regex to match URL in Powershell

분류에서Dev

Regex to match end of the word

분류에서Dev

.match() problems with regex

분류에서Dev

add apostrophe to regex match

분류에서Dev

RegEx to match list

분류에서Dev

Regex match in jQuery

분류에서Dev

std :: regex_match with characters é è à

분류에서Dev

Regex to match partial string verification

분류에서Dev

Append text to part of regex match

분류에서Dev

Regex match a string if its in the end

분류에서Dev

Recursively rename subdirectories that match a regex

분류에서Dev

Java regex match outside bracks

분류에서Dev

regex backtracking to value within match

분류에서Dev

Regex: Match Optional URI Parameters

분류에서Dev

Regex match for starting character or _\w