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: CopyCode The 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 ). The following is an example: copy Code the code is as follows: // Program , remove the domain name
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. Hope to see thisArticleIf you have already achieved this effect, you must leave a message for 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.