I have a string stored in a variable, say
myString= <RssFeeds<link>http://www.codeguru.com/icom_includes/feeds/codeguru/rss-all.xml</link>
<title>CodeGuru.com</title> <description>something</description></RssFeeds><RssFeeds><link>http://lifehacker.com/index.xml</link>
<title>Lifehacker</title>
<description>something</description>
</RssFeeds>
I want to extract the text between <link>and </link>
and also the text between <title>and </title>
in two different arrays.
How to do this, please help me. if anyone have code or any helpfull link please reply back.

Thanks

Dani AI

Generated

For XML-like input it's safest to parse rather than slice with fixed offsets or rely on fragile regular expressions. The substring approach shown by breaks as soon as any length changes, and ’s regex idea can work for very simple text but is brittle when tags contain nested HTML (the sample <link> contains an <a> tag). Note also the small copy/paste bug in ’s snippet: the title match should read the title group, not link.

A robust workflow:

  • Ensure the string is well-formed XML (wrap multiple top-level items in a single root or unescape HTML entities first).
  • Use a proper XML/HTML parser so nested tags and attributes are handled.
  • Extract either the inner text or, when present, the anchor’s href attribute inside <link>.

Example (Python): parse, unwrap entities, and prefer the <a href> value when present.

import xml.etree.ElementTree as ET
from html import unescape

xml = myString
if "&lt;" in xml or "&gt;" in xml:
    xml = unescape(xml)
xml = "<root>" + xml + "</root>"  # make a single root if needed

root = ET.fromstring(xml)
links, titles = [], []
for feed in root.findall(".//RssFeeds"):
    l = feed.find("link")
    t = feed.find("title")
    if l is not None:
        a = l.find("a")
        links.append(a.get("href").strip() if a is not None and "href" in a.attrib else "".join(l.itertext()).strip())
    if t is not None:
        titles.append("".join(t.itertext()).strip())

Example (JavaScript): use DOMParser and query selectors.

const doc = new DOMParser().parseFromString('<root>' + myString + '</root>', 'application/xml');
const feeds = Array.from(doc.getElementsByTagName('RssFeeds'));
const links = feeds.map(f => {
  const a = f.querySelector('link a');
  return a ? a.getAttribute('href') : (f.querySelector('link')?.textContent || '').trim();
});
const titles = feeds.map(f => (f.querySelector('title')?.textContent || '').trim());

If the input is not well-formed HTML/XML, prefer an HTML parser (BeautifulSoup, lxml, HtmlAgilityPack) instead of regex. This approach handles nested tags, attributes, and small encoding issues that substring/index tricks or ad-hoc regex tend to miss.

Recommended Answers

All 2 Replies

hi

Now Remember that i can only show you the door , you are the only person who have to walk throught it. i have created a substring and displayed them on the Messagebox, you have to assign that to the Array. here is the example code

String myString = @"<RssFeeds<link>http://www.codeguru.com/icom_includes/feeds/codeguru/rss-all.xml</link>
    <title>CodeGuru.com</title>   <description>something</description></RssFeeds><RssFeeds><link>http://lifehacker.com/index.xml</link>
    <title>Lifehacker</title>
    <description>something</description>
  </RssFeeds> ";
   MessageBox.Show(myString.Substring(15, 64)); /*It gave me [url]http://www.codeguru.com/icom_includes/feeds/codeguru/rss-all.xml[/url] */
   MessageBox.Show(myString.Substring(99,12)); //CodeGuru.com

Hope this Helps

Use a System.Text.RegularExpressions.Regex object.

Regex linkRegex = new Regex(@"<link>\s*(?<link>[^<]+)\s*</link>", System.Text.RegularExpressions.RegexOptions.Compiled);
            Regex titleRegex = new Regex(@"<title>\s*(?<title>[^<]+)\s*</title>", System.Text.RegularExpressions.RegexOptions.Compiled);

            if ( linkRegex.IsMatch(myString) ) {
                Match match = linkRegex.Match(myString);
                string theLink = match.Groups["link"].Value;
            }
            if ( titleRegex.IsMatch(myString) ) {
                Match match = titleRegex.Match(myString);
                string title = match.Groups["link"].Value;
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.