1

I have a text like this :

string text = "Lorem ipsum dolor sit [1] amet, [3] consectetuer adipiscing  [5/4/9] elit  
Ut odio. Nam sed est. Nam a risus et est[55/12/33/4] iaculis";

I want to get a list of string with all [digit] or all [digit/digit/...] present in my text.

For example:

{"[1]","[3]","[5/4/9]","[55/12/33/4]"} 

for the text above.

How can I do that with regex?

1
  • With regexes, it pays to heed the age-old advice in the spirit of your text: Omnia licent sed non omnia ædificant. Commented Nov 29, 2010 at 3:43

4 Answers 4

4
StringCollection resultList = new StringCollection();
Regex regexObj = new Regex(@"\[[\d/]*\]");
Match matchResult = regexObj.Match(subjectString);
while (matchResult.Success) {
    resultList.Add(matchResult.Value);
    matchResult = matchResult.NextMatch();
}

Explanation:

\[     # match a literal [
[\d/]* # match any number of digits or /
\]     # match a literal ]
Sign up to request clarification or add additional context in comments.

2 Comments

Why not use Matches() instead of Match()?
to get all ocurences of my patern
3

Get the matches as a collection, and get the value from each match into an array like this:

string[] items =
  Regex.Matches(text, @"\[([\d/]+)\]")
  .Cast<Match>()
  .Select(m => m.Groups[0].Value)
  .ToArray();

For simplicity the pattern matches anything that is digits and slashes between brackets, so it would also match something like [//42/]. If you need it to match only the exact occurances, you would need the more complicated pattern @"\[(\d+(?:/\d+)*)\]".

Edit:

Here is a version that works with framework 2.0.

List<string> items = new List<string>();
foreach (Match match in Regex.Matches(text, @"\[([\d/]+)\]")) {
  items.Add(m.Groups[0].Value);
}

7 Comments

it seem very good can you explain me the real difference of the Tim's answer?
@Christophe Debove: I use the Matches method to get all matches in a single call, then I use the Select method to get the value out from each of the matches.
I've just close visual studio but I will try it tomorow thanks
@Guffa Hi, it say Error 1 'System.Text.RegularExpressions.MatchCollection' does not contain a definition for 'Cast' and no extension method 'Cast' accepting a first argument of type 'System.Text.RegularExpressions.MatchCollection' could be found (are you missing a using directive or an assembly reference?), surely because I'm working on v2 on runtime
@Christophe Debove: You need to target framework 3.5 or later and have a using System.Linq; at the top of the file for this to work.
|
1

Assuming your input string is stored in a variable input:

Regex regex = new Regex("\\[(\\d/?)*\\]");
MatchCollection matches = regex.Matches(input);

MatchCollection matches holds all of the matches and can be displayed like this:

for (int i = 0; i < matches.Count; i++)
    Console.WriteLine(matches[i].Value);

The above loop outputs:

[1]
[3]
[5/4/9]
[55/12/33/4]

Comments

1
string regex = @"(\[[\d/]*?\])";

6 Comments

No need for a lazy quantifier or for capturing parentheses. And you should probably use a verbatim string.
True, there is no need for one, but I'd rather have one and not use it in contrast to not having one and forgetting to put it when I should change the expression.
I'm sorry, why the downvote? It wasn't clear he wanted the full C# code, he asked about regex, and that's what I gave him.
@Femaref the question was not only what is the regex I need, but how to get a list of string (it's in bold) with regex
Oh, okay. Didn't catch that then.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.