Reverse pre-search (top) _ Regular expression in regular expressions

Source: Internet
Author: User
In previous applications, most of the applications were in forward search. That is, there is a specific content qualification on the right side of the lookup content, such as the following example:
Copy Code code 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 lookup, only the right content is "; OS:" will be matched to.
But now to achieve the effect, the left side of the content is fixed, not to the right. However, JavaScript does not support reverse lookup. I believe that a careful friend has found that the above example has reached this goal (the previous memory\:\s+ defines the left side of the content). An example is given below:
Copy Code code as follows:

Program purpose, remove the domain name in the image path
var str = ' ';
var reg1 =/(\Str.match (REG1);
Alert (str.replace (regexp.$4, '));

So is there a reverse pre-search? The answer is yes, but the reality is brutal. I looked up some information, which mentioned the reverse pre-search in the form of <= or <!. As a guide. Unfortunately, JavaScript is not supported and has been supported in a later version of Java, so the following test program (JRE1.6.0_03) is written:
Copy Code code 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 Matcher = P.matcher ("System.out.println (Matcher.matches ());
}
}

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

Exception in thread ' main ' java.util.regex.patternsyntaxexception:look-behind group does not have an obvious maximum Th near Index 27
((? <=\
I google for a long time, the end of the only result is to change the way the wording. Hope to see this article friend, if already realized this kind of effect, must leave a message hesitate enlighten.

Also, when I look at the document, I see it: the regular expression of the boot, which says "match does not store", baffled. Feel or code to the reality, write a look at:
Copy Code code as follows:

var str2 = ' client name ';
var REG2 =/(client name (?: Said)?) /;
Str2.match (REG2);
alert (regexp.$1); Client Name
alert (regexp.$2); Empty string
Do not use?:
REG2 =/(client name (called)?) /;
Str2.match (REG2);
alert (regexp.$1); Client Name
alert (regexp.$2); Said

Agile development Theory says: Code is the best document. Gave me an excuse, hehe.

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.