1

I have string of the following format:

string test = "test.BO.ID";

My aim is string that part of the string whatever comes after first dot. So ideally I am expecting output as "BO.ID".

Here is what I have tried:

// Checking for the first occurence and take whatever comes after dot
var output = Regex.Match(test, @"^(?=.).*?"); 

The output I am getting is empty.

What is the modification I need to make it for Regex?

2 Answers 2

4

You get an empty output because the pattern you have can match an empty string at the start of a string, and that is enough since .*? is a lazy subpattern and . matches any char.

Use (the value will be in Match.Groups[1].Value)

\.(.*)

or (with a lookahead, to get the string as a Match.Value)

(?<=\.).*

See the regex demo and a C# online demo.

A non-regex approach can be use String#Split with count argument (demo):

var s = "test.BO.ID";
var res = s.Split(new[] {"."}, 2, StringSplitOptions.None);
if (res.GetLength(0) > 1)
    Console.WriteLine(res[1]);
Sign up to request clarification or add additional context in comments.

Comments

3

If you only want the part after the first dot you don't need a regex at all:

x.Substring(x.IndexOf('.'))

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.