0

I want to fill an object with records from a string where it matches a rule. The rule is if it contains at least 3 dots with a space to the right(". . . ") then I extract the first text to the left, the text that I just selected and its length.

string strdata = "Nume. . . . . . .Data nasterii. . . . .Nr. . . .";
Regex rgx = new Regex(". . . ");//At least 3 dots ". . . "
foreach (Match match in rgx.Matches(strdata))
    lst.Add(new obj1{ Label = "?", Value = match.Groups[1].Value, Length = match.Groups[1].Length });

I want to achive:

enter image description here

Q: What pattern do I have to use ?

2 Answers 2

1

You can use @"([\w\s]*)([\. ]{3,})" to achieve what I assume your trying to do.

That Regex will break out the text into the different groups and within those groups you will have the text value as well as the periods.

You can try it using this Regex Tester by inputing Nume. . . . . . .Data nasterii. . . . .Nr. . . . into the "Source" field and then entering in ([\w\s]*)([\. ]{3,}) into the "Pattern" field.

Sign up to request clarification or add additional context in comments.

1 Comment

If I had a "/" in my label ( "Nume/Prenume. . . . . Data nasterii. . . " ) then I would have to modify the pattern so it would take the whole group. I changed the pattern to ([\w\s/]*)([\. ]{3,}) and it worked.
1

In a regular expression, . means match any character. To match a ., you need to escape it as \.. To match 2 or more instances of an expression, use {2,}.

string strdata = "Nume. . . . . . .Data nasterii. . . . .Nr. . . .";
Regex rgx = new Regex(@"(.+?)(\.( \.){2,})");//At least 3 dots ". . . "
foreach (Match match in rgx.Matches(strdata))
{
    lst.Add(new obj1
    {
        Label = match.Groups[1].Value,
        Value = match.Groups[2].Value,
        Length = match.Groups[2].Length
    });
}

4 Comments

@RandomWebGuy - Yep, I forgot the ? in the first group, and was looking at groups 0 and 1 instead of 1 and 2. My latest edit should work.
Yeah. That seems to be closer. However it doesn't look like it's capturing all the periods right of the text? The first group for example only shows 4 periods? And then the next group has periods left of the text.
@RandomWebGuy - No, I don't see that. The Regex Tester you linked to shows the correct output.
You're right. I must have been looking at a previous Regex before one of your updates. My apologizes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.