Javascript-regular expressions match the content of the innermost brackets

Source: Internet
Author: User
Now there is a string: {code ...} or {code ...} I need to use regular expressions to match the innermost brackets in the string and the content (not matching the brackets in the quotation marks), that is, {code ...} so how should we write such ultra-complex regular expressions? If the regular expression cannot be implemented, how does javascript... now has a string:

str1 = '(subject_id = "A" OR (status_id = "Open" AND (status_id = "C" OR level_id = "D")))'

Or

str2 = '(subject_id = "A" OR subject_id = "Food" OR (subject_id = "C" OR (status_id = "Open" AND (status_id = "C" OR (level_id = "D" AND subject_id = "(Cat)")))))'

I need to use regular expressions to match stringsInnermost layerBrackets and their content (do not match the brackets in the quotation marks), that is:

str1 => (status_id = "C" OR level_id = "D")str2 => (level_id = "D" AND subject_id = "(Cat)")

So how should we write such ultra-complex regular expressions?

If regular expressions cannot be implemented, how can javascript be implemented?

Supplement:str1, I found such a regular expression to match:

\([^()]+\)

However, there is still no way for str2. We look forward to your answers!

Reply content:

Now there is a string:

str1 = '(subject_id = "A" OR (status_id = "Open" AND (status_id = "C" OR level_id = "D")))'

Or

str2 = '(subject_id = "A" OR subject_id = "Food" OR (subject_id = "C" OR (status_id = "Open" AND (status_id = "C" OR (level_id = "D" AND subject_id = "(Cat)")))))'

I need to use regular expressions to match stringsInnermost layerBrackets and their content (do not match the brackets in the quotation marks), that is:

str1 => (status_id = "C" OR level_id = "D")str2 => (level_id = "D" AND subject_id = "(Cat)")

So how should we write such ultra-complex regular expressions?

If regular expressions cannot be implemented, how can javascript be implemented?

Supplement:str1, I found such a regular expression to match:

\([^()]+\)

However, there is still no way for str2. We look forward to your answers!

For str2, I found this

\([^()]*\"[^"]*\"[^()]*\)

After reading the requirements, I didn't even consider using regular expressions. it seems that it is too complicated... just use the traditional method;
AvailableComputing priorityThe concept of "out-of-the-box"StackTo obtain the content of internal brackets;
Technical points:

  1. Matching the innermost brackets

  2. The content in the quotation marks is not used as a matching standard.

Follow this idea to design the algorithm:
This algorithm is used to calculate the substring to be matched.startIndexAndendIndexThen usesubstring()Method to obtain the substring;

  • When"("Character,Inbound stackWhen we matchFirst ")",Outbound stackThat is, the substring between two indexes is the target string;

  • Match to"\"", The match is stopped."("Until the next"\""To start searching."(".

If you have any shortcomings in this algorithm, please add it.

// Try this way
/\ ([^ \ (\)] *? "[^ \" \ (\)] * ([^ \ "\ (\)] + \) [^ \ (\)] *? \ "[^ \ (\)] *) +) | ([^ \ (\)] + \)/

Supplement:

Analyze requirements> Find a solution for each requirement point> integrated solution = solution

Analysis requirements:
  1. Need to match( a )Format

  2. WhereaThere are two possible characters.a1Anda2Indicates

    1. a1Contains one or moreb " c " bString,

      1. WherebIs a segment not included",(Or)String

      2. WherecIs a segment not included"String

    2. a2Does not include(Or)

Inverse derivation:

2.2 =>a2=[^\(\)]*
2.1.1 =>b=[^\(\)\"]*
2.1.2 =>c=[^\"]*
2.1 =>a1=(b\"c\"b)+=(b\"c\")+b=([^\(\)\"]*\"[^\"]*\")+[^\(\)\"]*
1 =>\(a\)=\(a1\)|\(a2\)=\(([^\(\)\"]*\"[^\"]*\")+[^\(\)\"]*\)|\([^\(\)]*\)

Regular expression:
/\(([^\(\)\"]*\"[^\"]*\")+[^\(\)\"]*\)|\([^\(\)]*\)/
Verification:
var reg = /\(([^\(\)\"]*\"[^\"]*\")+[^\(\)\"]*\)|\([^\(\)]*\)/;'(the (quick "brown" fox "jumps over, (the) lazy" dog ))'    .match(reg)[0]//"(quick "brown" fox "jumps over, (the) lazy" dog )"'(the ("(quick)" brown fox "jumps (over, the)" lazy) dog )'    .match(reg)[0];//"("(quick)" brown fox "jumps (over, the)" lazy)"'(the (quick brown fox (jumps "over", ((the) "lazy"))) dog )'    .match(reg)[0];//"(the)"

That's the change:

substr=str.match(/\([^()]+\)/g)[0]

Obtain the value in the innermost brackets, and then judge whether the first digit of the value is "and the last digit is ":

index=str.indexOf(str.match(/\([^()]+\)/g)[0])length=str.match(/\([^()]+\)/g)[0].lengthstr.substr(index+length,1)str.substr(index-1,1)

If not, the answer is required. If yes, replace the substr in str first, match it, and then replace it back:

str.replace(substr,"&&&")str.replace(substr,"&&&").match(/\([^()]+\)/g)[0]str.replace(substr,"&&&").match(/\([^()]+\)/g)[0].replace("&&&",substr)

The difficulty of this question is to perform recursive statistics on "", for example

(level_id = "D AND subject_id = "(Cat)"")

(Cat) is compliant.

\([^()]*?\"((?:[^\"\"]|\"(?1)\")*+)\"[^()]*?\)|\([^()]*?\)

True love life, away from regular expressions. this regular expression can meet your requirements. php can use (php supports recursion) and java and Python cannot use it.

We recommend that you use the following method to locate (index, split string processing ).

No regular black lines are displayed on the mobile phone
Continue if () does not match in [^ ()] of the landlord
Remove the non-match (condition, and change greedy + *? You can.

! Code

Console. log ('(subject_id = "A" OR (status_id = "Open" AND (status_id = "C" OR level_id = "D ")))'. match (/(1 *)/))
Hope to help you

  1. ()

Regular Expression Matching is complicated. we recommend that you replace the interference string "(and)", for example, "[,]", with a simple regular expression, and then change it back.

Regular expressions are implemented in Python as follows:

import restr1 = '(subject_id = "A" OR (status_id = "Open" AND (status_id = "C" OR level_id = "D")))'str2 = '(subject_id = "A" OR subject_id = "Food" OR (subject_id = "C" OR (status_id = "Open" AND (status_id = "C" OR (level_id = "D" AND subject_id = "(Cat)")))))'pat = re.compile(r"""(?<=[^"])        \([^()]+?        ("\(.+?\)")*        \)        (?=[^"])        """, re.X)print pat.search(str1).group(0)print pat.search(str2).group(0)

Output:

(status_id = "C" OR level_id = "D")(level_id = "D" AND subject_id = "(Cat)")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.