开发者

How to match a regex pattern with a string by providing it a starting line no. and ending line no. within which the search should be performed?

开发者 https://www.devze.com 2022-12-22 11:13 出处:网络
I am writing a small windows script in javascript/jscript for finding a ma开发者_JAVA技巧tch for a regexp with a string that i got by manipulating a file.

I am writing a small windows script in javascript/jscript for finding a ma开发者_JAVA技巧tch for a regexp with a string that i got by manipulating a file.

I know the line number for starting and ending of the search. But I am stuck at this position for a while.


You would need to match all the lines preceding the bit you're interested in, then capture the part you want in a capturing group. For example, to extract the fourth and fifth lines, you could use this regex:

/^(?:.*(?:\r\n|[\r\n])){3}((?:.*(?:\r\n|[\r\n])){2})/

The part you want will be available in capturing group #1.


The best way to look at something like this is to not overcomplicate your regular expression - you could make it rather unreadable and confusing, particularly if your regex skills aren't very good. Simplify things first - split the document into an array of lines, pick out your line and then perform your regular expression. Running your regular expression on a smaller string in this fashion could prove to be much faster on very large text files.

// Assuming file is a FileSystemObject TextStream object:
var lines = file.ReadAll().split("\r\n"); // split based on Windows newline

// Arrays have zero-based indexes, so the line you need is your line number -1
var match = lines[4].match(myRegEx);      // lines[4] is line 5 of the string

// If you need it to span multiple lines, extract and join the lines you need:
var specificLines = lines.splice(4, 6);   // extract the 5th and 6th lines 
specificLines.match(myRegEx);             // don't forget the regex's /m modifier

splice returns the array items from the starting index upto and excluding the ending index. You can also provide a limit parameter to the split method to stop splitting after a certain number of matches. This should improve speed on a large file where the lines you need aren't near the end.

Glossary:

  • splice Method (Array)
  • split Method
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号