Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a string between 2 known values

Tags:

c#

.net

regex

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.

like image 484
Ashley Avatar asked Nov 11 '09 19:11

Ashley


People also ask

How do you find the string between two characters?

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.

How to extract a string between two words in Python?

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.

How to extract a part of string between two characters in Python?

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.

How do I extract text from two characters in R?

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.


2 Answers

  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.

like image 125
Aaron Avatar answered Sep 22 '22 16:09

Aaron


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); } 
like image 20
mmx Avatar answered Sep 25 '22 16:09

mmx