I need to be able to extract a string between 2 tags for example: "00002" from "morenonxmldata<tag1>0002</tag1>morenonxmldata
"
I am using C# and .NET 3.5.
To get a substring between two characters:Get the index after the first occurrence of the character. Get the index of the last occurrence of the character. Use the String. slice() method to get a substring between the 2 characters.
A Quick Guide to Using split() This function can be used to split strings between characters. The split() function takes two parameters. The first is called the separator and it determines which character is used to split the string. The split() function returns a list of substrings from the original string.
To get the substring between two characters in a string with Python, there are a few ways you can do it. The easiest way is with the index() function and string slicing. You can also use the regular expression re module to get the substring between two characters.
Extracting a String Between 2 Characters in R Find the position of the final character and subtract 1 from it – that is the final position of the desired string. Use the substr() function to extract the desired string inclusively between the initial position and final position as found in Steps 1-2.
Regex regex = new Regex("<tag1>(.*)</tag1>"); var v = regex.Match("morenonxmldata<tag1>0002</tag1>morenonxmldata"); string s = v.Groups[1].ToString();
Or (as mentioned in the comments) to match the minimal subset:
Regex regex = new Regex("<tag1>(.*?)</tag1>");
Regex
class is in System.Text.RegularExpressions
namespace.
Solution without need of regular expression:
string ExtractString(string s, string tag) { // You should check for errors in real-world code, omitted for brevity var startTag = "<" + tag + ">"; int startIndex = s.IndexOf(startTag) + startTag.Length; int endIndex = s.IndexOf("</" + tag + ">", startIndex); return s.Substring(startIndex, endIndex - startIndex); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With