0

i have this kind of string File type: Wireshark - pcapng so what i want is if my string start with File type: take and parse only Wireshark - pcapng

this is what i have try:

var myString = @":\s*(.*?)\s* ";

3 Answers 3

7

Instead of REGEX, use string.StartsWith method, something like:

if(str.StartsWith("File type:"))
   Console.WriteLine(str.Substring("File type:".Length));

You will get:

 Wireshark - pcapng

If you want to get rid of leading/trailing spaces from the resultant string then use string.Trim like:

Console.WriteLine(str.Substring("File type:".Length).Trim());

Or if you just want to get rid of leading spaces then use string.TrimStart like:

Console.WriteLine(str.Substring("File type:".Length).TrimStart(' '));
Sign up to request clarification or add additional context in comments.

3 Comments

You should also remove the space after :.
received Wireshark - pcapng, with empty characters at the beginning
@user2214609, use string.Trim, check the latest answer
1

Why don't you just remove File type: from your string:

str = str.Replace("File type: ",string.Empty);

Or you can check if the string starts with File type: and remove that part using string.Remove():

if(str.StartsWith("File type: "){
    str=str.Remove(11); //length of "File Type: "
}

Comments

0

This should do the trick:

(?<=^File type: ).*$

so...

var match = Regex.Match("File type: Wireshark - pcapng", @"(?<=^File type: ).*$");
if(match.Success)
{
    var val = match.Value;
    Console.WriteLine(val);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.