1

I am having strings like below

<ad nameId="\862094\"></ad>

or comma seprated like below

<ad nameId="\862593\"></ad>,<ad nameId="\862094\"></ad>,<ad nameId="\865599\"></ad>

How to extract nameId value and store in single string like below

string extractedValues ="862094";

or in case of comma seprated string above

string extractedMultipleValues ="862593,862094,865599";

This is what I have started trying with but not sure

string myString = "<ad nameId="\862593\"></ad>,<ad nameId="\862094\"></ad>,<ad         
                    nameId="\865599\"></ad>";
string[] myStringArray = myString .Split(',');
foreach (string str in myStringArray )
{

   xd.LoadXml(str);
   chkStringVal = xd.SelectSingleNode("/ad/@nameId").Value;
}
4

4 回答 4

2

Search for:

<ad nameId="\\(\d*)\\"><\/ad>

Replace with:

$1

Note that you must search globally. Example: http://www.regex101.com/r/pL2lX1

于 2013-07-26T14:18:30.400 回答
1

Try like this;

string s = @"<ad nameId=""\862094\""></ad>";
if (!(s.Contains(",")))
{

    string extractedValues = s.Substring(s.IndexOf("\\") + 1, s.LastIndexOf("\\") - s.IndexOf("\\") - 1);
}
else
{
    string[] array = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    string extractedMultipleValues = "";
    for (int i = 0; i < array.Length; i++)
    {
         extractedMultipleValues += array[i].Substring(array[i].IndexOf("\\") + 1, array[i].LastIndexOf("\\") - array[i].IndexOf("\\") - 1) + ",";
    }

    Console.WriteLine(extractedMultipleValues.Substring(0, extractedMultipleValues.Length -1));
}
于 2013-07-26T14:25:08.967 回答
1

mhasan, here goes an example of what you need(well almost)

EDITED: complete code (it's a little tricky)

enter image description here

(Sorry for the image but i have some troubles with tags in the editor, i can send the code by email if you want :) )

A little explanation about the code, it replaces all ocurrences of parsePattern in the given string, so if the given string has multiple tags separated by "," the final result will be the numbers separated by "," stored in parse variable....

Hope it helps

于 2013-07-26T15:00:03.297 回答
1

Please see code below to extract all numbers in your example:

   string value = @"<ad nameId=""\862093\""></ad>,<ad nameId=""\862094\""></ad>,<ad nameId=""\865599\""></ad>";
   var matches = Regex.Matches(value, @"(\\\d*\\)", RegexOptions.RightToLeft);
   foreach (Group item in matches)
   {
     string yourMatchNumber = item.Value;
   }
于 2013-07-26T15:12:22.757 回答