Find wildcard matches in string

xdzgor

New member
Joined
Jan 7, 2014
Messages
3
Programming Experience
5-10
Hi, I have strings which include "tags" like "[ref:123]" or "[ext:456]".

An example string could be
One of the aircraft [ref:123] crashed near the Peruvian border [ext:456] due to a malfunctioning windspeed indicator [ext:888].

Is there a simple wildcard search which would allow me to easily find all the tags - something like Find("[*:*]")?
That is, find all the tags which start with '[', end with ']', and contain two text-strings separated by ':'.

Thanks.
 
You can use regular expressions, implemented in .NET via the Regex class. I don't use regular expressions much myself so I'm never quite sure of the correct syntax but, if noone else answers and you don't figure it out for yourself, I'll try to get back to you with the solution.
 
You could do this without regular expressions as well:

C#:
string FindString(string s, string tag) {
   var tags = "[" + tag + "]";
   int startIndex = s.IndexOf(tags) + tags.Length;
   int endIndex = s.IndexOf("[" + tag + "]", startIndex);
   
   return s.Substring(startIndex, endIndex - startIndex);
}
 
You could do this without regular expressions as well:

C#:
string FindString(string s, string tag) {
   var tags = "[" + tag + "]";
   int startIndex = s.IndexOf(tags) + tags.Length;
   int endIndex = s.IndexOf("[" + tag + "]", startIndex);
   
   return s.Substring(startIndex, endIndex - startIndex);
}
xdzgor asked for wildcard search a la [*:*] to "find all the tags".

Regular expression example: \[.+?:.+?]
Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
 
Back
Top Bottom