Implement reverse pre-search in Regular Expressions

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: <Br/> var str3 = 'cpu: Intel Core2 5200; Memory: 2G; OS: Ubuntu 9.04 '; <br/> var reg3 =/Memory \: \ s + (. *(? =\; OS \ :))/gim; <br/> str3.match (reg3); <br/> alert (RegExp. $1); // The result is 2 GB <br/>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

(.*(? = \; 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.