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?
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.
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.
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.
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.
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.
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