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.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,919
Location
Sydney, Australia
Programming Experience
10+
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.
 

bthomassuco

New member
Joined
Jan 23, 2014
Messages
2
Programming Experience
3-5
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);
}
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,562
Location
Norway
Programming Experience
10+
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
 
Top Bottom