Reverse pre search in regular expressions


In the past, most of the applications are forward pre search. In other words, there are specific content restrictions on the right side of the search content, such as 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;
alert(RegExp.$1); // The result is 2G

(.*(?=\; OS \:) is a typical forward pre search, only the content on the right side is “; OS: “will be matched.
But now to achieve the effect, the content of the left side is fixed, not the right side. However, JavaScript does not support reverse pre search. I believe that careful friends have found that the above example has already achieved this goal (the memory in front is limited to the content on the left). Here is another example:

Copy codeThe code is as follows:
//The purpose of the program is to remove the domain name in the image path
var str = ‘<img src=”” >’;
var reg1 = /(\<img)(.*(?=(http|https)\:\/\/))((http|https)\:\/\/[^\/]*)/gim;

So is there reverse pre search? The answer is yes, but the reality is cruel. I have consulted some materials. What is the reverse pre search mode mentioned above<= Or? <! For guidance. Unfortunately, JavaScript is not supported. It has been supported in higher versions of Java, so we 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(“((?<=\\<img.*(?=(http|https)))(http|https))”);
        Matcher matcher = p.matcher(“<img src=””/>”);

There should be no problem with the program itself, but the running result is as follows:

Exception in thread “main” java.util.regex.PatternSyntaxException: Look-behind group does not have an obvious maximum length near index 27

I googled for a long time, and the only result I got was a different way of writing. Hope to see this article friends, if you have achieved this effect, be sure to leave a message.

In addition, when I look at the document, I see?: Guided regular expression, the above said is “matching does not store”, can’t think of its solution. I feel that the code is real. Let’s take a look at it

Copy codeThe code is as follows:
Var STR2 =’Client name ‘;
Var reg2 = / (client name (?): (1) /;
alert(RegExp.$1); // Client name
alert(RegExp.$2); // Empty string
//Not used?:
Reg2 = / (client name) /;
alert(RegExp.$1); // Client name
alert(RegExp.$2); // call

Agile development thought says: code is the best document. Give me an excuse, ha ha.

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]