简体   繁体   中英

Regex find text before some characters

I have big files like this:

f = """
(function ($hx_exp, $g) { 
var A = function() { };
$hxCls[1] = $hxCls["A"] = A;
A.__name__ = ["A"];
A.__get = function(d,e) {
     return "a"; 
};
var B = function(a,c) {
    this.r = new Func1(a,c);
};
$hxCls[2] = $hxCls["B"] = B;
B.__name__ = ["B"];
B.prototype = {
   r: null,
   map: function(s,f) { return 0; },
   __class__: B
};
}(this));
"""

I want split this text into like this:

var A = function() { };
$hxCls[1] = $hxCls["A"] = A;
A.__name__ = ["A"];
A.__get = function(d,e) {
        return "a"; 
};

I use regexp:

re.findall(r"(^var.+)", txt)

But I fund only lines:

var A = function() { };

var B = function(a,c) {

Maybe someone knows libraries that can break such large JavaScript files into several others.

Try this:

re.finditer(r"var\s\w\s=.*?(?=var\sB|\}\(this\))", txt, re.DOTALL)

Test: https://regex101.com/r/2O6rgN/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM