A way to use RegEx to find a set of filenames paths in a string

后端 未结 3 1154
长情又很酷
长情又很酷 2021-02-09 18:12

Good morning guys

Is there a good way to use regular expression in C# in order to find all filenames and their paths within a string variable?

For e

3条回答
  •  旧巷少年郎
    2021-02-09 18:46

    If you use tag and the final text could be represented as well formatted xml document (as far as being inner xml, i.e. text without root tags), you probably can do:

    var doc = new XmlDocument();
    doc.LoadXml(String.Concat("", input, ""));
    
    var files = doc.SelectNodes("//file"):
    

    or

    var doc = new XmlDocument();
    
    doc.AppendChild(doc.CreateElement("root"));
    doc.DocumentElement.InnerXml = input;
    
    var nodes = doc.SelectNodes("//file");
    

    Both method really works and are highly object-oriented, especially the second one.

    And will bring rather more performance.

    See also - Don't parse (X)HTML using RegEx

提交回复
热议问题