开发者

Separating some signs from other signs in a string

Trying To reprase I want to split "(" ")" "&&" "||" from my string

my original question: Separating numbers from other signs in a string I use开发者_JS百科 this example

string text = "(54&&1)||15";
Regex pattern = new Regex(@"\(|\)|&&|\|\||\d+");
Match match = pattern.Match(text);
while (match.Success)
{
Console.WriteLine(match.Value);
match = match.NextMatch();
}

my problem is that I don't always get a proper string with only numbers and the signs I might get a wierd string like:

0ò8F&|&&booBl||aBla

I want it to be seperated to

0ò8F&| && booBl || aBla


The reason it is not working is that, assuming you are using the regular expression provided by Jon Skeet in your accepted answer, you are not looking for that formulation in your string. The regex given was:

\(|\)|&&|\|\||\d+

this looks for:

NODE                     EXPLANATION
  \(                       '('
 |                        OR
  \)                       ')'
 |                        OR
  &&                       '&&'
 |                        OR
  \|\|                     '||'
 |                        OR
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))

(courtesy of http://rick.measham.id.au/paste/explain.pl) with slight modifications.

As you can see there is no way this is going to match "&" or "1&2" so if you want it to you need to include these values in the regular expression.

This regex relies on your original statement:

I got a string that contains:

"(" ")" "&&" "||"

and numbers (0 to 99999).

The single & in your string that is failing doesn't match the above. Is it the case in fact that you can have the above four string tokens and then any other text or are there still limitations like numbers and optional single &? If so then this is actually a different problem from the original...

If you wish to use those four things as delimiters on arbitrary text then Regex.Split may be the way forward (http://msdn.microsoft.com/en-us/library/8yttk7sy.aspx).

string[] results = Regex.Split(@"(54&1&1)||15", @"(\()|(\))|(&&)|(\|\|)");

The above should split the first string by the four specified delimiters ("(" ")" "&&" "||") and ouptut all the split strings and the delimiters themselves. One issue you may have with this is that if two delimiters are adjacent then you will get an empty value in the array that you will need to take into account.

The results of the above are:

0 - 
1 - (
2 - 54&1&1
3 - )
4 -  
5 - ||
6 - 15

Also this will not work on .NET 1.0 or 1.1 but I hope that won't be a problem.

It should be noted that the regular expression has been changed slightly to enclose all the captures in brackets to make them capturing groups and to remove the \d+ group which was just used for anything that wasn't the delimiters.

To remove the blank entries from the array all you need is a line of code like:

results = results.Where(x=>!String.IsNullOrEmpty(x)).ToArray();

Of course you can leave it as an IEnumberable or similar if you don't actually need it as an array.


   List<string> sList = new List<string>();
    Regex pattern = new Regex(@"\(|\)|&&|\|\||\d+");
    Match match = pattern.Match(text);
    while (match.Success)
    {
        sList.Add(match.Value);
        //Console.WriteLine(text2[nCounter]);
        match = match.NextMatch();

    }string text = "(54&&1)||15";     

sList contain your desired output


Did you try the answer I gave to your original question?

See: Separating numbers from other signs in a string

It solves your problem.

var operators = new [] { "(", ")", "&&", "||", };

Func<string, IEnumerable<string>> operatorSplit = t =>
{
    Func<string, string, IEnumerable<string>> inner = null;
    inner = (p, x) =>
    {
        if (x.Length == 0)
        {
            return new [] { p, };
        }
        else
        {
            var op = operators.FirstOrDefault(o => x.StartsWith(o));
            if (op != null)
            {
                return (new [] { p, op }).Concat(inner("", x.Substring(op.Length)));
            }
            else
            {
                return inner(p + x.Substring(0, 1), x.Substring(1));
            }
        }
    };
    return inner("", t).Where(x => !String.IsNullOrEmpty(x));
};

var list = operatorSplit("0ò8F&|&&booBl||aBla").ToList();

Result: { "0ò8F&|", "&&", "booBl", "||", "aBla" }


This will do what you are asking:

@"\(|\)|&&|\|\||.+?(?=\(|\)|&&|\|\||$)"

It substitutes the \d+ character set in your previous pattern with .+?(?=\(|\)|&&|\|\||$) which uses a positive lookahead to instruct the engine to capture everything up to, but not including, your operator tokens.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜