Regular expression n-related issues

Source: Internet
Author: User
n Problems with regular expressions
Well, as mentioned in the post of the previous days, I've started to learn the regular.
I have been groping for 32 days, and now some of them are not clear questions
I am now in the hands of this book is not very detailed, I am on the Baidu encyclopedia to learn
Words return to positive turn
=====================================
=====================================
Question 1:
I would like to ask that regular expressions usually need to be used

/wrap content? /

And if there is a modifier, the modifier is written after the second/number, right? (such as/aaa/i with I as a modifier Fu Caifang 2nd/number after)
I am looking at the Baidu encyclopedia to learn, it said a paragraph:

$
Matches the line terminator. For example, the regular expression weasel$ is able to match the end of the string "He's a Weasel"
However, the string "They is a bunch of weasels" cannot be matched.


So I used PHP to write down the code debugging,
$str 2= "He s a Weasel";
$PM 2= ' weasel$ ';

if (Preg_match ($pm 2, $str 2))
{
Echo ' True
';
}
Else
{
Echo ' False
';
}

But it turned out to be a failure.
After a long time of tinkering, I finally put
$PM 2= ' weasel$ ';
Change into
$PM 2= '/weasel$/';

Success, but the encyclopedia did not say so.
Maybe I didn't really look at anything.
In this confirmation, the content of the search is wrapped around the left and right, the modifier is after the 2nd/number is it? I don't even know the rules of writing. Don't say the matching rules.


=====================================
=====================================
Question 2:
The first in the encyclopedia refers to the "meta-character" of the table, the fourth line refers to the role of the *, with. * Match I understand, but then it says, "like .* Can match no matter what it is "

So I tested it with the above code, but I didn't find it.
~~~~~~~~~~~~~~~~~~~~~~
$str 1=<< <>

Test

$PM 1 = '/ .* /';

if (Preg_match ($pm 1, $str 1))
{
Echo ' 1. Found
';
}
Else
{
Echo ' 1. Not found
';
}



~~~~~~~~~~~~~~~~~~~~~~
Here, it says. .* Contained in/, I realized/could lead to being mistaken for an expression terminator, and then changed to/, but also not, and in fact, I understand its description as: The contents of this Can be .* This expression matches, but I don't know how to write the expression.

=====================================
=====================================
Question 3:
I'm actually going to interpret it as an escape character, right? It's not very clear in the encyclopedia. Like in C or PHP, \ n is escaped as a carriage return,
\ t is the tab tab, \ \ \ \ \ \ \ \$ is $ or something.


=====================================
=====================================
Question 4:
The following code is based on the 7th line of the encyclopedia character Cell, ' for example, a regular expression \ Ability to match "the" in the string "for the Wise"

$str 1=<< <>
For the Wise
Test

$PM 1 = '/\ /';
if (Preg_match ($pm 1, $str 1))
{
Echo ' 1. Found
';
}
Else
{
Echo ' 1. Not found
';
}

The reason for this is not because he added, "This meta-character is not supported by all software."

=====================================
=====================================
Question 5:
About? The introduction of the number, Baidu completely do not understand, and looked for other documents
I understand it as a app?path? The app in front of the number three letters that appear in front of the path can match, but I do not understand the setting character

$str 1=<< <>
Abcdefg
Test

$PM 1= '/df?e/';
if (Preg_match ($pm 1, $str 1))
{
Echo ' 1. Found
';
}
Else
{
Echo ' 1. Not found
';
}

But also can match! Where there is DF Ah, even a f is behind the E, here began to make me feel like a white learning, as if not understand the regular matching rules!




------Solution--------------------
Half a look. That's how you understand.

/tell the regular condition to start the second/tell the regular regular condition ended. That's it. Actually this is just a combination you can even use # #或者其他字符来代替这个所谓的开始和结束.

In this case, if/xxxxa/xxxxb/this time, it will be thought that when the XXXXA is over, then it will go wrong, and at this time we need to escape to let it know that it is just a character in the middle rather than the end. That's why it's used to escape.

$str 1=<< <>

Test

$PM 1= '/ . * /';

The match condition here is to start with any character and then until the end of your str1 no End he certainly does not match.
The rest of you don't see, let others tell you haha
------Solution--------------------
Question one:
/Here is the real regular/
Regular all need to be wrapped in the middle, if there is a modifier, add the last/back
such as: Preg_match ('/aaa$/is ') here is the modifier, as to what each means, you have to check
Of course here to notice,//In the package of the regular if also contain/this thing, need to escape the
Also note, here is a//wrapped in a regular, you can also use the #@ in the bag, you can try

Question two:
Of course you can't find it here, you're looking for with the As well as the middle content, you give the content only " ", certainly does not meet the requirements, can not find is normal, found that is your RP problem.

Question three:
Like the question I said, you use/wrap the regular, if your regular also appear/, of course to escape, and, $^? Such are special characters, you want to directly match that special character, of course, you have to escape, otherwise the computer will be special characters to deal with
Example:
Content: AAABBB$CCC
If you want to match bb$cc whether it fits
1:/bb$cc/This of course is problematic to see the literal is consistent with the above, but the computer will take $ this as the last meaning
2:/bb\ $CC/This is the right one.
  • 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.