Regular expression talents:
Separator (),/,//
(A) B: B, a B are all correct. the words in brackets are optional.
(A/B) c: c, a c, and B c are all correct./The relation between the first and second words is OR.
A // B: both a and B are correct.
Requirements:
Convert the combination of words represented by the above symbol into a regular expression
Example: (it is/'s) the/a) cat // (It is/'s) (the) animal
Reply to discussion (solution)
Note:
Separator (),/,//
(A) B: B, a B are all correct. the words in brackets are optional.
(A/B) c: c, a c, and B c are all correct./The relation between the first and second words is OR.
A // B: both a and B are correct.
Requirements:
Convert the combination of words represented by the above symbol into a regular expression
Example: (it is/'s) the/a) cat // (It is/'s) (the) animal
I don't quite understand what you mean
First (a) B matches the character "AB" and the sub-string ""
(A/B) c matches the character "a/bc" and the substring "a/B"
Again, a // B matches the string "a // B"
(It is/'s) the/a) cat // (It is/'s) (the) animal
You want to write a regular expression to match
It is a cat
It is the cat
It's a cat
It's the cat
It is animal
It is the animal
It's animal
It's the animal
Right?
(It ('s | \ s + is) \ s + (a | the) \ s + cat) | (it ('s | \ s + is) \ s + (animal | the \ s + animal ))
I don't quite understand what you mean
First (a) B matches the character "AB" and the sub-string ""
(A/B) c matches the character "a/bc" and the substring "a/B"
Again, a // B matches the string "a // B"
(It is/'s) the/a) cat // (It is/'s) (the) animal
You want to write a regular expression to match
It is a cat
It is the cat
It's a cat
It's the cat
It is animal
It is the animal
It's animal
It's the animal
Right?
In addition to the above,
Cat
A cat
The cat
Animal
The animal
All are correct.
I want a conversion method, separator (), //, // These are agreed rules, not regular semantics. thank you for your reply on the first floor.
Make it easy to understand
Function pregRule ($ str) {$ str = trim ($ str); return '/^ '. str_replace (array (')', '[', ']', '//', '/', ''), array (')? \ S * ',' (',') {1} ',' | ',' | ',' \ s + '), $ str ). '$/';} $ str = "(a) B"; echo pregRule ($ str ).'
'; $ Str = "[a/B]"; # Add brackets to convert echo pregRule ($ str ).'
'; $ Str = "(a/B) c"; echo pregRule ($ str ).'
'; $ Str = "(it [is/'s]) the/a) cat // (It [is/'s]) (the) animal "; echo pregRule ($ str ).'
';
# Result:
/^ ()? \ S * B $/
/^ (A | B) {1} $/
/^ (A | B )? \ S * c $/
/^ (It \ s + (is |'s) {1 })? \ S * the | )? \ S * cat \ s + | \ s + (It \ s + (is |'s) {1 })? \ S * ()? \ S * animal $/
The first three types of functions work together, and the fourth match is hard to bear. Please help enrich this function to adapt it and modify the statement as appropriate.
Make it easy to understand
Function pregRule ($ str) {$ str = trim ($ str); return '/^ '. str_replace (array (')', '[', ']', '//', '/', ''), array (')? \ S * ',' (',') {1} ',' | ',' | ',' \ s + '), $ str ). '$/';} $ str = "(a) B"; echo pregRule ($ str ).'
'; $ Str = "[a/B]"; # Add brackets to convert echo pregRule ($ str ).'
'; $ Str = "(a/B) c"; echo pregRule ($ str ).'
'; $ Str = "(it [is/'s]) the/a) cat // (It [is/'s]) (the) animal "; echo pregRule ($ str ).'
';
# Result:
/^ ()? \ S * B $/
/^ (A | B) {1} $/
/^ (A | B )? \ S * c $/
/^ (It \ s + (is |'s) {1 })? \ S * the | )? \ S * cat \ s + | \ s + (It \ s + (is |'s) {1 })? \ S * ()? \ S * animal $/
The first three types of functions work together, and the fourth match is hard to bear. Please help enrich this function to adapt it and modify the statement as appropriate.
Probably understood
Why? are you sure you are familiar with these so-called rules?
You want to make a function similar to automatic marking. I guess the teacher who sets the answer will be confused.
Make it easy to understand
Function pregRule ($ str) {$ str = trim ($ str); return '/^ '. str_replace (array (')', '[', ']', '//', '/', ''), array (')? \ S * ',' (',') {1} ',' | ',' | ',' \ s + '), $ str ). '$/';} $ str = "(a) B"; echo pregRule ($ str ).'
'; $ Str = "[a/B]"; # Add brackets to convert echo pregRule ($ str ).'
'; $ Str = "(a/B) c"; echo pregRule ($ str ).'
'; $ Str = "(it [is/'s]) the/a) cat // (It [is/'s]) (the) animal "; echo pregRule ($ str ).'
';
# Result:
/^ ()? \ S * B $/
/^ (A | B) {1} $/
/^ (A | B )? \ S * c $/
/^ (It \ s + (is |'s) {1 })? \ S * the | )? \ S * cat \ s + | \ s + (It \ s + (is |'s) {1 })? \ S * ()? \ S * animal $/
The first three types of functions work together, and the fourth match is hard to bear. Please help enrich this function to adapt it and modify the statement as appropriate.
The fourth major problem is to replace "/" with "|"
A/B
Ca/bd
In the first case, replace "/" with "|" instead of thinking too much.
In the second case, you have to consider it. Do you want to get cad, cbd, or ca or bd? Therefore, you cannot replace "/" with "|"
The following is your fourth expression after replacement.
/^ (It \ s + (is |'s) {1 })? \ S * the | )? \ S * cat \ s + | \ s + (It \ s + (is |'s) {1 })? \ S * ()? \ S * animal $/
Only read the first half
(It \ s + (is |'s) {1 })? \ S * the | )? In \ s * cat \ s +
(It \ s + (is |'s) {1 })? \ S * the |)
Do you want it \ s + (is |'s) {1 })? \ S * matched characters and then concatenate the or
But what actually appears in the expression is that you want it \ s + (is |'s) {1 })? \ S * the or
You want I want (the | a), but you actually say I want the |