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?
original title: "parsing - Regex: To pull out a sub-string between two tags in a string"
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?
मेरे पास निम्न प्रारूप में एक फ़ाइल है: डेटा डेटा डेटा [प्रारंभ] डेटा मैं चाहता हूं [अंत] डेटा मैं एक आरजेएक्स का उपयोग करके [स्टार्ट] और [एंड] टैग के बीच से जो डेटा चाहता हूं, उसे हथियाना चाहता हूं। क्या कोई मुझे दिखा सकता है ...
यह अनुवाद के बाद का सारांश है, अगर आपको पूरा अनुवाद देखने की आवश्यकता है, तो कृपया 'अनुवाद' आइकन पर क्लिक करें
This should hopefully drop the
[start]
and[end]
markers as well.Zhich'll put the text in the middle within a capture.
I had a similar problem for a while & I can tell you this method works...
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:
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:
you probably don't want to capture:
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)
Well, if you guarantee that each start tag is followed by an end tag then the following would work.
However, If you have complex text such as the follwoing:
then you would run into problems with regex.
Now the following example will pull out all the hot links in a page:
In the above case we can guarantee that there would not be any nested cases of:
So, this is a complex question and can't just be solved with a simple answer.
With Perl you can surround the data you want with ()'s and pull it out later, perhaps other languages have a similar feature.
Refer to this question to pull out text between tags with space characters and dots (
.
)[\S\s]
is the one I usedRegex to match any character including new lines
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/