Regex: To pull out a sub-string between two tags i

2019-01-16 14:08发布

I have a file in the following format:

Data Data
Data
[Start]
Data I want
[End]
Data

I'd like to grab the Data I want from between the [Start] and [End] tags using a Regex. Can anyone show me how this might be done?

标签: regex parsing
9条回答
霸刀☆藐视天下
2楼-- · 2019-01-16 14:24

Well, if you guarantee that each start tag is followed by an end tag then the following would work.

\[start\](.*?)\[end\]

However, If you have complex text such as the follwoing:

[start] sometext [start] sometext2 [end] sometext [end]

then you would run into problems with regex.

Now the following example will pull out all the hot links in a page:

'/<a(.*?)a>/i'

In the above case we can guarantee that there would not be any nested cases of:

'<a></a>'

So, this is a complex question and can't just be solved with a simple answer.

查看更多
对你真心纯属浪费
3楼-- · 2019-01-16 14:32

With Perl you can surround the data you want with ()'s and pull it out later, perhaps other languages have a similar feature.

if ($s_output =~ /(data data data data START(data data data)END (data data)/) 
{
    $dataAllOfIt = $1;      # 1 full string
    $dataInMiddle = $2;     # 2 Middle Data
    $dataAtEnd = $3;        # 3 End Data
}
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-16 14:33

Reading the text with in the square brackets [] i.e.[Start] and [End] and validate the array with a list of values. jsfiddle http://jsfiddle.net/muralinarisetty/r4s4wxj4/1/

var mergeFields = ["[sitename]",
                   "[daystoholdquote]",
                   "[expires]",
                   "[firstname]",
                   "[lastname]",
                   "[sitephonenumber]",
                   "[hoh_firstname]",
                   "[hoh_lastname]"];       

var str = "fee [sitename] [firstname] \
sdfasd [lastname] ";
var res = validateMeargeFileds(str);
console.log(res);

function validateMeargeFileds(input) {
    var re = /\[\w+]/ig;
    var isValid;
    var myArray = input.match(re);

    try{
        if (myArray.length > 0) {
            myArray.forEach(function (field) {

                isValid = isMergeField(field);

                if (!isValid){
                   throw e;                        
                }
            });
        }
    }
    catch(e) {        
    }

    return isValid;
}

function isMergeField(mergefield) {
    return mergeFields.indexOf(mergefield.toLowerCase()) > -1;
}
查看更多
聊天终结者
5楼-- · 2019-01-16 14:34

Refer to this question to pull out text between tags with space characters and dots (.)

[\S\s] is the one I used

Regex to match any character including new lines

查看更多
Summer. ? 凉城
6楼-- · 2019-01-16 14:37
\[start\](.*?)\[end\]

Zhich'll put the text in the middle within a capture.

查看更多
别忘想泡老子
7楼-- · 2019-01-16 14:41
$text ="Data Data Data start Data i want end Data";
($content) = $text =~ m/ start (.*) end /;
print $content;

I had a similar problem for a while & I can tell you this method works...

查看更多
登录 后发表回答