user3453883
user3453883

Reputation: 29

Regex String in C#

I have a string

"Máy Tính Acer[Asprise 4741 | 058941144] - 302";
"Máy Tính Acer[Asprise 4741 | 058941145] - 302";

Now I want to use Regex to get 2 string result:

058941144

058941145

Upvotes: 1

Views: 79

Answers (1)

Szymon
Szymon

Reputation: 43023

If you can use look-ahead and look-behind assertions, you can use this regex to match between | and ]:

(?<=\|\s)[0-9]+(?=])

In C#, you can use this code:

String input = "Máy Tính Acer[Asprise 4741 | 058941144] - 302";
String pattern = @"(?<=\|\s)[0-9]+(?=])";

var match = Regex.Match(input, pattern).ToString();

If you want to match bases on the whole string you put in your question, you can use a longer regex that works in basically the same way as the one above:

String pattern = @"(?<=Máy Tính Acer\[Asprise\s4741\s\|\s)[0-9]+(?=]\s-\s302)";

For a more generic pattern that will match any charaters, use

String pattern = @"(?<=\|).+(?=])";

Upvotes: 2

Related Questions