Reverse pre-search in regular expressions __ regular expressions

Source: Internet
Author: User

These days in a regular expression to do a small program, you need to replace some of the special strings in the content. The occurrence of these strings is regular, that is, there will always be a specific content on the left, but not the right. So naturally the idea of a reverse search.

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:

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:

Program purpose to remove the domain name var str in the image path = ' '; var reg1 =/(/

So is there any 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:

Import Java.util.regex.Matcher; Import Java.util.regex.Pattern; public class Regexptest {public static void main (string[] args) {Pattern p = pattern.compile ("() <=//

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:

var str2 = ' client name '; var REG2 =/(client name (?: Said)?) /; Str2.match (REG2); alert (regexp.$1); Client name alert (regexp.$2); empty string//Not used?: 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.