Kai
Kai

Reputation: 2987

Regex to extract digits followed by specific word

Using Regex, I want to extract digits which are followed by a specific word.

The number of digits is not finite.

Sample input:

My address is [email protected] and you can send SMS to me.

Expected Result.

1234

In this case, the specific word is @abc.com, and the digits followed by this word need to be extracted.

Upvotes: 1

Views: 385

Answers (3)

9000
9000

Reputation: 40904

.* (\d+)@abc\.com .* should work.

Upvotes: 0

driis
driis

Reputation: 164341

You will need to match [email protected] and use a grouping to extract the digits:

(\d+)\@abc.com

Upvotes: 2

LaGrandMere
LaGrandMere

Reputation: 10379

Use the regular expression groups : on MSDN.

In C#, try this :

string pattern = @"(\d+)@abc\.com";
string input = "My address is [email protected] and you can send SMS to me";
Match match = Regex.Match(input, pattern);

// Get the first named group.
Group group1 = match.Groups[1];
Console.WriteLine("Group 1 value: {0}", group1.Success ? group1.Value : "Empty");

Upvotes: 4

Related Questions