1

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

6
  • 1
    What is the pattern? What is the language you use? Commented Mar 24, 2014 at 3:18
  • Is it always "Máy Tính Acer[Asprise 4741 | " and then "] - 302"? Commented Mar 24, 2014 at 3:21
  • Is allways that. and input have a multi that. Commented Mar 24, 2014 at 3:28
  • the input is : "Name [Model|ID] - Room"; Commented Mar 24, 2014 at 3:33
  • \d{9} if the number is always 9 digits and the only 9 digit number. Commented Mar 24, 2014 at 3:34

1 Answer 1

2

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 = @"(?<=\|).+(?=])";
Sign up to request clarification or add additional context in comments.

4 Comments

Did you use the code above? What is your input? Is it many lines?
the input that : "Name [Model|ID] - Room";
Well, that won't work as you specified digits in this position. See my update now.
@Szymon I had deleted my answer, and +1 for your working code

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.