The regular reaches the person to come in

Source: Internet
Author: User
Description
Delimiter (),/,//

(a) B:b,a B is correct. The words in parentheses are optional
(A/b) C:c,a c,b c are correct./before and after the word is or the relationship
A//b:a,b are correct

Requirements:
Converts the combination of words represented by the above symbols into regular notation
Example: ((it is/' s) the/a) CAT//(it is/' s) (the) Animal


Reply to discussion (solution)

Description:
delimiter (),/,//

(a) B:b,a B are correct. The words in parentheses
(A/b) C:c,a c,b c are correct./before and after the word is or off
A//b:a,b are correct

Requirements:
Convert the combination of words represented above into regular notation
Example: ((it is/'s) the/a) CAT//(it is/' s) (the) Animal

don't quite understand what you want to say

First (a) B matches the character "AB", the substring "a"
followed (A/b) C matches the character "A/BC", 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 match
It is a cat
it's the cat br> It's a cat
it's the cat
It is animal
It's the animal
It's animal
It's the animal
?

(It (' S|\s+is) \s+ (a|the) \s+cat) | ( It (' S|\s+is) \s+ (animal|the\s+animal))

Don't quite understand what you're trying to say.

First (a) B matches the character "AB", the substring "a"
Second (A/b) C matches the character "A/BC", 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 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
, huh?
Except for those above,
Cat
A cat
The Cat
Animal
The animal
are right.
I want a conversion, a delimiter (),/,//These are convention rules, not regular semantics. Thank you, 1 floor.


Make an example 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]"; #加个中括号, easy 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:
/^ (a)? \s*b$/
/^ (a|b) {1}$/
/^ (a|b)? \s*c$/
/^ (it\s+ (is| ' s) {1})? \s*cat\s+|\s+ (it\s+ (is| ' \s*the|a)? s) {1})? \s* (The)? \s*animal$/

The first three kinds of still do, the fourth match will not bear the heart to see, please help enrich this function, so that it can be adapted to appropriate changes in the statement

Make an example 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]"; #加个中括号, easy 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:
/^ (a)? \s*b$/
/^ (a|b) {1}$/
/^ (a|b)? \s*c$/
/^ (it\s+ (is| ' s) {1})? \s*cat\s+|\s+ (it\s+ (is| ' \s*the|a)? s) {1})? \s* (The)? \s*animal$/

The first three kinds of still do, the fourth match will not bear the heart to see, please help enrich this function, so that it can be adapted to appropriate changes in the statement

Probably understood.

Why, are you sure that these so-called rules users you set are understood?

You want to do a function like automatic grading, I guess the teacher who set the answer will be confused.

Make an example 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]"; #加个中括号, easy 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:
/^ (a)? \s*b$/
/^ (a|b) {1}$/
/^ (a|b)? \s*c$/
/^ (it\s+ (is| ' s) {1})? \s*cat\s+|\s+ (it\s+ (is| ' \s*the|a)? s) {1})? \s* (The)? \s*animal$/

The first three kinds of still do, the fourth match will not bear the heart to see, please help enrich this function, so that it can be adapted to appropriate changes in the statement

The fourth major problem is to replace the words "/" with "|"

A/b
Ca/bd
In the first case, there is no need to think too much directly replace "/" with "|" Just OK.
In the second case, you have to think about it. Do you want to CAD,CBD or want CA,BD? So this time, you can't just consider "/" and replace it with "|".

Here's your fourth expression after the substitution
/^ (it\s+ (is| ' s) {1})? \s*cat\s+|\s+ (it\s+ (is| ' \s*the|a)? s) {1})? \s* (The)? \s*animal$/
Just look at the first half.
((It\s+ (is| ' s) {1})? \s*the|a)? \s*cat\s+ in the
(It\s+ (is| ' s) {1})? \s*the|a)
You want it\s+ (is| ' s) {1})? \s* matches the characters and then stitching the or a
But actually the expression is showing that you want it\s+ (is| ' s) {1})? \s*the or a

You want I want (the|a), but actually you say I want the|a
  • 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.