2

I have a string and I want to get the words after the last dot . in the string.

Example:

string input = "XimEngine.DynamicGui.PickKind.DropDown";

Result:

DropDown
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Andrei Călugăr
  • 143
  • 1
  • 3
  • 14

3 Answers3

7

There's no need in Regex, let's find out the last . and get Substring:

 string result = input.Substring(input.LastIndexOf('.') + 1);

If input doesn't have . the entire input will be returned.

Edit: Same code rewritten with a help of range:

string result = input[(input.LastIndexOf('.') + 1)..]; 

Finally, if you insist on regular expression, you can put it as

string result = Regex.Match(input, "[^.]*$", RegexOptions.RightToLeft).Value;

We match zero or more symbols which are not dot . starting from the end of the string (from the right).

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
4

Not a RegEx answer, but you could do:

var result = input.Split('.').Last();
pappbence96
  • 1,164
  • 2
  • 12
  • 20
0

In Regex you can tell the parser to work from the end of the string/buffer by specifying the option RightToLeft.

By using that we can just specify a forward pattern to find a period (\.) and then capture (using ( )) our text we are interested into group 1 ((\w+)).

var str = "XimEngine.DynamicGui.PickKind.DropDown";
Console.WriteLine(Regex.Match(str, 
                              @"\.(\w+)", 
                              RegexOptions.RightToLeft).Groups[1].Value);

Outputs to console:

DropDown

By working from the other end of the string means we don't have to deal with anything at the beginning of the string to where we need to extract text.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122