Reverse pre-search (top) in Regular Expression)

Source: Internet
Author: User

In the past, most of the applications were pre-searches. That is to say, there are specific content limits on the right side of the search content, for example, the following example:
Copy codeThe Code is as follows:
Var str3 = 'cpu: Intel Core2 5200; Memory: 2G; OS: Ubuntu 9.04 ';
Var reg3 =/Memory \: \ s + (.*(? = \; OS \ :))/gim;
Str3.match (reg3 );
Alert (RegExp. $1); // The result is 2G

(.*(? = \; OS \ :) is a typical forward pre-search. Only "; OS:" On the right will be matched.
However, the content on the left is fixed, not on the right. However, JavaScript does not support reverse pre-search. I believe that careful friends have discovered that the above example has achieved this goal (the previous Memory \: \ s + limits the content on the left ). Here is another example:
Copy codeThe Code is as follows:
// Program purpose, remove the domain name in the image path
Var str = ' ';
Var reg1 =/(\ Str. match (reg1 );
Alert (str. replace (RegExp. $4 ,''));

Is there any reverse pre-search? The answer is yes, but the reality is cruel. I checked some materials. The reverse pre-search format mentioned above is as follows? <= Or? <! For guidance. Unfortunately, JavaScript is not supported and has been supported in the high version of Java, so I wrote the following test program (JRE1.6.0 _ 03 ):
Copy codeThe Code is as follows:
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
Public class RegExpTest {
Public static void main (String [] args ){
Pattern p = Pattern. compile ("((? <=\\ Matcher = p. matcher (" ");
System. out. println (matcher. matches ());
}
}

The program itself should have no problems, but the running results:

Exception in thread "main" java. util. regex. PatternSyntaxException: Look-behind group does not have an obvious maximum length near index 27
((? <=\
I spent half a day on Google, and the only result I got was a new one. If you want to see this article, you must leave a message to us.

What else do I see when I read the document? : The guiding regular expression, which says "matching is not stored", cannot be understood. I feel like the code is true. Let's take a look:
Copy codeThe Code is as follows:
Var str2 = 'client name ';
Var reg2 =/(client name (? )?) /;
Str2.match (reg2 );
Alert (RegExp. $1); // client name
Alert (RegExp. $2); // empty string
// Do not use? :
Reg2 =/(client name (name )?) /;
Str2.match (reg2 );
Alert (RegExp. $1); // client name
Alert (RegExp. $2); // name

In Agile development, code is the best document. Gave me an excuse.

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.