Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect particular tokens in a string. C#

Tags:

string

c#

.net

I have a very large string (HTML) and in this HTML there is particular tokens where all of them starts with "#" and ends with "#"

Simple Eg

<html>
<body>
      <p>Hi #Name#, You should come and see this #PLACE# - From #SenderName#</p>
</body>
</html>

I need a code that will detect these tokens and will put it in a list. 0 - #Name# 1 - #Place# 2 - #SenderName#

I know that I can use Regex maybe, anyway have you got some ideas to do that?

like image 652
David Bonnici Avatar asked Nov 25 '10 13:11

David Bonnici


People also ask

What does strtok () do in C?

The C function strtok() is a string tokenization function that takes two arguments: an initial string to be parsed and a const -qualified character delimiter. It returns a pointer to the first character of a token or to a null pointer if there is no token.

Does strtok return a string?

Returned valueThe first time strtok() is called, it returns a pointer to the first token in string1. In later calls with the same token string, strtok() returns a pointer to the next token in the string. A NULL pointer is returned when there are no more tokens. All tokens are NULL-terminated.

Why is strtok used?

Practical Application: strtok can be used to split a string in multiple strings based on some separators. A simple CSV file support might be implemented using this function.

How do you get next token in strtok?

The strtok() function gets the next token from string s1, where tokens are strings separated by characters from s2. To get the first token from s1, strtok() is called with s1 as its first parameter. Remaining tokens from s1 are obtained by calling strtok() with a null pointer for the first parameter.


1 Answers

You can try:

// using System.Text.RegularExpressions;
// pattern = any number of arbitrary characters between #.
var pattern = @"#(.*?)#";
var matches = Regex.Matches(htmlString, pattern);

foreach (Match m in matches) {
    Console.WriteLine(m.Groups[1]);
}

Answer inspired in this SO question.

like image 153
Pablo Santa Cruz Avatar answered Sep 30 '22 15:09

Pablo Santa Cruz