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

后端 未结 9 1934
情书的邮戳
情书的邮戳 2020-12-08 00:35

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

相关标签:
9条回答
  • 2020-12-08 00:45
    $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...

    0 讨论(0)
  • 2020-12-08 00:46

    While you can use a regular expression to parse the data between opening and closing tags, you need to think long and hard as to whether this is a path you want to go down. The reason for it is the potential of tags to nest: if nesting tags could ever happen or may ever happen, the language is said to no longer be regular, and regular expressions cease to be the proper tool for parsing it.

    Many regular expression implementations, such as PCRE or perl's regular expressions, support backtracking which can be used to achieve this rough effect. But PCRE (unlike perl) doesn't support unlimited backtracking, and this can actually cause things to break in weird ways as soon as you have too many tags.

    There's a very commonly cited blog post that discusses this more, http://kore-nordmann.de/blog/do_NOT_parse_using_regexp.html (google for it and check the cache currently, they seem to be having some downtime)

    0 讨论(0)
  • 2020-12-08 00:47
    \[start\](.*?)\[end\]
    

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

    0 讨论(0)
  • 2020-12-08 00:47

    A more complete discussion of the pitfalls of using a regex to find matching tags can be found at: http://faq.perl.org/perlfaq4.html#How_do_I_find_matchi. In particular, be aware that nesting tags really need a full-fledged parser in order to be interpreted correctly.

    Note that case sensitivity will need to be turned off in order to answer the question as stated. In perl, that's the i modifier:

    $ echo "Data Data Data [Start] Data i want [End] Data" \
      | perl -ne '/\[start\](.*?)\[end\]/i; print "$1\n"'
     Data i want 
    

    The other trick is to use the *? quantifier which turns off the greediness of the captured match. For instance, if you have a non-matching [end] tag:

    Data Data [Start] Data i want [End] Data [end]
    

    you probably don't want to capture:

     Data i want [End] Data
    
    0 讨论(0)
  • 2020-12-08 00:47

    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
    }
    
    0 讨论(0)
  • 2020-12-08 00:48

    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;
    }
    
    0 讨论(0)
提交回复
热议问题