Sora
Sora

Reputation: 2551

get specific string pattern

I have the following string :

string str ="dasdsa|cxzc|12|#dsad#|czxc";

I need a function that return : #dsad#

Taking under consideration that #dsad# is created dynamically so it may vary and it's length may vary also

How can i do that using regular expression (searching for the string between the two hashtags) or other method if available ?

Upvotes: 0

Views: 135

Answers (5)

user6882413
user6882413

Reputation: 341

According to the input given in the Question that you need to search for the string between the two hashtags following will be the regex,

^.*#(.*)#.*$

If the data is empty between 2 hashtags, still the regex will not fail. It will take empty value.

Upvotes: 2

ΩmegaMan
ΩmegaMan

Reputation: 31616

This appears to be CSV delimited data with five sections of data.

Extract each of the sections and then project into a dynamic entity using regex to denote each of the sections.

string data = "dasdsa|cxzc|12|#dsad#|czxc";

string pattern = @"(?<Section1>[^|]+)\|(?<Section2>[^|]+)\|(?<Section3>[^|]+)\|(?<Section4>[^|]+)\|(?<Section5>[^|]+)";

var results =
Regex.Matches(data, pattern, RegexOptions.ExplicitCapture)
     .OfType<Match>()
     .Select(mt => new
     {
            One = mt.Groups["Section1"].Value,
            Two = mt.Groups["Section2"].Value,
            Three = mt.Groups["Section3"].Value,
            Four = mt.Groups["Section4"].Value,
            Five = mt.Groups["Section5"].Value,
     })
     .ToList();

    Console.WriteLine(results[0].Four ); // #dsad#

Once complete extract from what results contains. For it is just a list of the multi-line captures, where each line contains just is one entity of data for that line.

Extract from the appropriate properties; whereas our final result on has one line, but we can get to the data as shown in the example WriteLine:

enter image description here

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626806

If your string is a pipe-delimited string, you may split with |, and grab the value that starts and ends with #.

var str ="dasdsa|cxzc|12|#dsad#|czxc";
var result = str.Split('|').Where(p => p.StartsWith("#") && p.EndsWith("#")).ToList();
foreach (var s in result)
    Console.WriteLine(s);

See the online C# demo.

If you only need one value, use .FirstOrDefault():

var result = str.Split('|')
   .Where(p => p.StartsWith("#") && p.EndsWith("#"))
   .FirstOrDefault();

Upvotes: 2

Mong Zhu
Mong Zhu

Reputation: 23732

Assuming that #dasd# appears only once in the string try this:

String a = "asd|asd|#this#|asdasd";


string m = Regex.Match(a, @"#.+#").Value;

Console.WriteLine(m);

.+ searches for any character

IF your string is using the | as delimiter, you could also split the string.

string [] array = a.Split('|');

// this would get a list off all values with `#`
List<string> found_all = array.Where(x => x.Contains("#")).ToList();

// this would get the first occurrence of that string. If not found it will return null
string found = array.FirstOrDefault(x => x.Contains("#"))

Upvotes: 2

merl
merl

Reputation: 162

Assuming you want to match on the # character you can take the following regex:

#[a-z]+#

  • "[]" character set
  • "+" one character or more

If you want your string is always of the form 1|2|3|#4#|5 you could use the String.Split('|') method and just take the fourth element of the result.

Upvotes: 2

Related Questions