0

I'm building a regex and I'm missing something as it's not working properly. my regex logic is trying to look for anything that has #anychars# and return the number of matches on the sentence and not a single match. Here are a few examples

1- #_Title_# and #_Content_# should return two matches: #_Title_# and #_Content_#.

2- Product #_TemplateName_# #_Full_Product_Name_# more text. text text #_Short_Description_# should return 3 matches: #_TemplateName_# #_Full_Product_Name_# and #_Short_Description_#

and so on. Here is what my regex looks like: ^(.*#_.*_#.*)+$

any thoughts on what I'm doing wrong?

2
  • Are you matching #anychars# of #_anychars_# Commented Oct 13, 2016 at 17:15
  • I updated the question as the formating was misleading. Commented Oct 13, 2016 at 17:20

4 Answers 4

2

Something as simple as:

#.*?# 

Or:

#_.*?_# 

If you are trying to match the underscores too (it wasn't clear in the original version of the question). Or:

#_(.*?)_# 

Which makes it easier to extract the token between your #_ and _# delimiters as a group.

Should work. The *? is key. It's non-greedy. Otherwise you match everything between the first and last #

So for example:

var str = "Product #_TemplateName_# #_Full_Product_Name_# more text. text text #_Short_Description_#"; var r = new Regex("#_(.*?)_#"); foreach (Match m in r.Matches(str)) { Console.WriteLine(m.Value + "\t" + m.Groups[1].Value); } 

Outputs:

#_TemplateName_#     TemplateName #_Full_Product_Name_#    Full_Product_Name #_Short_Description_#    Short_Description 
Sign up to request clarification or add additional context in comments.

Comments

1

Try this :

 string[] inputs = { "#Title# and #Content#", "Product #TemplateName# #_Full_Product_Name_# more text. text text #_Short_Description_#" }; string pattern = "(?'string'#[^#]+#)"; foreach (string input in inputs) { MatchCollection matches = Regex.Matches(input, pattern); Console.WriteLine(string.Join(",",matches.Cast<Match>().Select(x => x.Groups["string"].Value).ToArray())); } Console.ReadLine(); 

Comments

1

You regular expression is not correct. In addition, you want to loop through match if you want all matching.

static void Main(string[] args) { string input = "Product #_TemplateName_# #_Full_Product_Name_# more text. text text #_Short_Description_#", pattern = "#_[a-zA-Z_]*_#"; Match match = Regex.Match(input, pattern); while (match.Success) { Console.WriteLine(match.Value); match = match.NextMatch(); } Console.ReadLine(); } 

Result

enter image description here

Comments

0

Don't use anchors and change your regex to:

(#[^#]+#)

In regex the [^#] expression means any character BUT #

using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string pattern = @"(#[^#]+#)"; Regex rgx = new Regex(pattern); string sentence = "#blah blah# asdfasdfaf #somethingelse#"; foreach (Match match in rgx.Matches(sentence)) Console.WriteLine("Found '{0}' at position {1}", match.Value, match.Index); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.