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:
Matching the innermost brackets
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.startIndex
AndendIndex
Then 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:
Need to match( a )
Format
Wherea
There are two possible characters.a1
Anda2
Indicates
a1
Contains one or moreb " c " b
String,
Whereb
Is a segment not included"
,(
Or)
String
Wherec
Is a segment not included"
String
a2
Does 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
- ()
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)")