开发者

Help me to split string with Regular Expression

CriteriaCondition={FieldName=**{**EPS**}**$MinValue=(-201)$MaxValue=(304)$Trading开发者_StackOverflow中文版Period=(-1)}

Help me to get the first word which ends with the first word "={" & get the next following word which ends with "}".

The result must be:

Word1 = "CriteriaCondition"
Word2 = "FieldName={EPS}$MinValue=(-201)$MaxValue=(304)$TradingPeriod=(-1)"

And with the string "FieldName=(EPS)$MinValue=(-201)$MaxValue=(304)$TradingPeriod=(-1)", help me to split to pairs:

FieldName EPS

MinValue -201

MaxValue 304

TradingPeriod -1

Thanks.


This looks like a job for .NET captures. In contrast to many other regex flavors, .NET remembers all captures of a repeated capturing group, not just the last one. I don't have VS installed here, so I can't test this yet, but try the following:

Match match = Regex.Match(subjectString, 
    @"(.*?)     # Match any number of characters and capture them
    =\{         # Match ={
    (           # Match and capture the following group:
     (?:        # One or more occurences of the following:
      (?>[^$]+) # One or more characters except $, match possessively and capture
      \$?       # Match the delimiting $ (optionally, because it's not there after the last item)
     )+         # End of repeating group
    )           # End of capturing group
    \}          # Match }
    ", 
    RegexOptions.IgnorePatternWhitespace);
Console.WriteLine("Matched text: {0}", match.Value);
Console.WriteLine("Word1 = \"{0}\"", match.Groups[1].Value);
Console.WriteLine("Word2 = \"{0}\"", match.Groups[2].Value);    
captureCtr = 0;
foreach (Capture capture in match.Groups[3].Captures) {
    Console.WriteLine("      Capture {0}: {1}", 
                             captureCtr, capture.Value);
    captureCtr++; 
}


The regex for the first split is: ^([^=]+)={(.*)}$

It containts: - start of line - a first group of any character except = (Word1) - the characters ={ - the remaining of the string (Word2) - the character } - end of line

Then you can split Word2 into parts separated by the character $, and apply a similar regex (without { and }) to each part


This regex will get you most of the way there (and let you get a hold of the stuff you're interested in using named capture groups

@"(?<criteria>[^=]+)\=\{((?<params>[^$]+)(\$|}))+"

However, I assume a lot of the information in the string you don't actually care about so it would be better to just get rid of it outright and then parse the string from there.

Something along these lines would very clear about what exactly you're trying to accomplish (if you ever need to go back and make changes to it):

var temp = "CriteriaCondition={FieldName=**{**EPS**}**$MinValue=(-201)$MaxValue=(304)$TradingPeriod=(-1)}";

var startToken = "={";
var paramString = temp.Substring( temp.IndexOf( startToken ) + startToken.Length );
var cleanParamString = Regex.Replace( paramString, @"[*{}()]", "");

var parameters = cleanParamString.Split('$');

would give you a string array with the following rows

FieldName=EPS
MinValue=-201
MaxValue=304
TradingPeriod=-1

and you can manipulate them much more easily from there.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜