-3

I have a string ---TIMESTAMP Tue, 24 Oct 2017 02:11:56 -0400 [1508825516987]---

I want to get the value within the [] (i.e. 1508825516987)

How can I get the value using Regex?

Yotam Salmon
  • 2,400
  • 22
  • 36
Karthikeyan
  • 299
  • 1
  • 10
  • 25
  • 3
    Please show what you've already tried. It should be fairly simple to capture values within `[` and `]`. (It's not clear that you actually want a split - it looks like you just want to match and then extract the value from a group.) – Jon Skeet Dec 09 '17 at 13:52
  • Tried (TIMESTAMP [a-zA-Z])+ but no luck. I know this is wrong and I am learner in the Regex. – Karthikeyan Dec 09 '17 at 13:58
  • So show the exact code that you tried, and the results rather than just "didn't work". – Jon Skeet Dec 09 '17 at 13:59
  • (Edit the question rather than just putting it in comments.) – Jon Skeet Dec 09 '17 at 13:59
  • Regex regex = new Regex(@"\[(.*?)\]", RegexOptions.IgnoreCase | RegexOptions.Singleline); Use [RegExr](https://regexr.com) to learn to create regular expressions. – Odrai Dec 09 '17 at 13:59
  • 2
    Possible duplicate of [Regular expression to extract text between square brackets](https://stackoverflow.com/questions/2403122/regular-expression-to-extract-text-between-square-brackets) – Odrai Dec 09 '17 at 13:59
  • @Jon Skeet I tried this code string test = "---TIMESTAMP Tue, 24 Oct 2017 02:11:56 -0400 [1508825516987]---"; string pattern = @"TIMESTAMP\s(*?)"; Regex regex = new Regex(pattern); Match match = regex.Match(test); string result = match.Success ? match.Groups[1].Value : ""; – Karthikeyan Dec 09 '17 at 14:00
  • The answer is in the duplicated question, see @Odrai comment. Also http://regexstorm.net/ is a handy site for testing regular expressions. – D. Foley Dec 09 '17 at 14:03

2 Answers2

2

Explanation:

  • \[ : [ is a meta char and needs to be escaped if you want to match it literally.
  • (.*?) : match everything in a non-greedy way and capture it.
  • \] : ] is a meta char and needs to be escaped if you want to match it literally.

Source of explanation: Click

static void Main(string[] args)
{
    string txt = "---TIMESTAMP Tue, 24 Oct 2017 02:11:56 -0400 [1508825516987]---";

    Regex regex = new Regex(@"\[(.*?)\]", RegexOptions.IgnoreCase | RegexOptions.Singleline);
    Match match = regex.Match(txt);
    if (match.Success)
    {
        for (int i = 1; i < match.Groups.Count; i++)
        {
            String extract = match.Groups[i].ToString();
            Console.Write(extract.ToString());
        }
    }
    Console.ReadLine();
}

Links to learn to create regular expressions:

Update 1:

Regex regex = new Regex(@"^---.*\[(.*?)\]", RegexOptions.IgnoreCase | RegexOptions.Singleline);
  • ^ is start of string
  • --- are your (start) characters
  • .* is any char between --- and [
Odrai
  • 2,163
  • 2
  • 31
  • 62
1

You can use the following code to get the outcome what you want!

 MatchCollection matches = Regex.Matches("---TIMESTAMP Tue, 24 Oct 2017 02:11:56 -0400 [1508825516987]---", @"\[(.*?)\]", RegexOptions.Singleline);
 Match mat = matches[0];
 string val = mat.Groups[1].Value.ToString();

whereas the string val will contain the value what you required.

Ethiraj
  • 117
  • 1
  • 12
  • ---TIMESTAMP Tue, 24 Oct 2017 02:11:56 -0400 [1508825516987]--- How can I get this value 24 Oct 2017 02:11:56 from this string by using regex. – Karthikeyan Mar 28 '18 at 13:13
  • You can get this value 24 Oct 2017 02:11:56 by using this @"\,(.*?)-" unicode character which can separate the content which you needed by using regex. – Ethiraj May 15 '18 at 12:54