Click here to Skip to main content
15,891,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a string with 0 and 1 charachers.How do i get the positons of substrings(start and end index of each substring in a input string) that matched with a given pattern: 1*100*11*.
(11*==111111111... ,00*==0000000....)

example:
regexp pattern=1*100*11*

C#
string str="10100011101101"
index1={0,2};//101
index2={2,8};//1000111
index3={6,11};//111011
index4={10,13};//1101
Posted
Updated 15-Oct-15 7:51am
v3
Comments
Andreas Gieriet 15-Oct-15 12:56pm    
What exactly is the semantic of your pattern? What exactly identifies a "substring"? Assuming the * stands for "any character", then in C# Regex, this would be [01]*. But still, you don't say where a substring starts and ends. In C# Regex, this is given by (...).
Please clarify.
Regards
Andi
Member 11276287 15-Oct-15 13:44pm    
Thank you for reply.
How to doing it? do you have a solution for doing it?
Andreas Gieriet 15-Oct-15 15:00pm    
I see that your pattern is to be interpreted as C# regex. Ok.
What is strange is that you do not search for consecutive matches but for overlapping matches.
Search for the first match, then cut the first char and search for the next match, etc. With that approach, you have to avoid duplicate matches at the same positions, though.
Regards
Andi

That isn't what Regexes are all about - they find a match, then discard all the characters that form part of the match, and then check again on what is left.

They don't return "all possible matches" for a single input.
So your example input and match string will only get two matches:
101
111011
because the first match is removed, leaving only "00011101101" to scan again.

I suspect that you need to write your own rather more complicated code to find them all, or find the first, discard the first matched character, and then run the regex again until you run out of matches.
 
Share this answer
 
You need to learn about RegEx, read documentation, try tutos.
The documentation will give you all functions associated with regEx.

To test an expression against a string, you can try https://www.debuggex.com/[^]

It may be easier to get start position and length than end position, but you can deduce the last one from the 2 first.
 
Share this answer
 
The following hack produces a result similar to your examples:
C#
string str = "10100011101101";
Regex rex = new Regex(@"1*100*11*");
Console.WriteLine("string str=\"{0}\"", str);
int lastend = -1;
for (int i = 0; i < str.Length; i++)
{
    Match match = rex.Match(str.Substring(i));
    if (match.Success)
    {
        int pos = match.Index + i;
        int len = match.Length;
        int end = pos + len - 1; // may result in end < pos if len == 0
        if (lastend < end)
        {
            Console.WriteLine("index={{{0},{1}}};//{2}", pos, end, match.Value);
        }
        lastend = end;
    }
}
How to tell the instructor how this works? ;-)
string str="10100011101101"
index={0,2};//101
index={2,8};//1000111
index={6,11};//111011
index={10,13};//1101
Regards
Andi
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900