Andrii Shyriaiev
Andrii Shyriaiev

Reputation: 277

C# Regex how to get number

how to get phone number drom next files name

  1. 09-33-07 [A 9312886109902] 'DN_9405~' (44,-0,0,0).wav
  2. 08-47-51 [A 9309854699902] 'DN_9405~' (44,-0,0,0).wav
  3. 07-58-49 [P 9160] 'DN_9405~' (44,-0,0,0) .wav

I need to get in one step

  1. 931288610
  2. 930985469
  3. 9160

now i use (\d{4,})[^(9902])] but on short numbers is wrong

Upvotes: 0

Views: 100

Answers (4)

Avinash Raj
Avinash Raj

Reputation: 174696

You could use the below regex which matches upto 9902 or ] symbol,

(?<=[A-Z] ).*?(?=9902|])

DEMO

OR

(?<=[A-Z] )\d+?(?=9902|])

DEMO

Upvotes: 0

Braj
Braj

Reputation: 46841

You can try with Lookaround

(?<=\[[A-Z] )\d+?(?=9902\]|\])

online demo and tested at regexstorm

Pattern explanation:

  (?<=                     look behind to see if there is:
    [[A-Z]                   any character of: '[', 'A' to 'Z'

  )                        end of look-behind
  \d+?                     digits (0-9) (1 or more times)
  (?=                      look ahead to see if there is:
    9902]                    '9902]'
   |                        OR
    ]                        ']'
  )                        end of look-ahead

Upvotes: 0

Piotr Stapp
Piotr Stapp

Reputation: 19830

You are searching for square bracket, than letter, than space, than 1+ digits, than quare bracket. So the regular expression is: [\[][A-Z].\d+[\]]

But because you want to ezxtract only 1+ digits (number) you need to group them using (), so regex is [\[][A-Z].(\d+)[\]]

Then the rest of the code is simple:

Regex regexp = new Regex("[\[][A-Z].(\d+)[\]]");
foreach(var mc in qariRegex.Matches(yourstring))
{
    Console.Writeln(mc[0].Groups[1].Value);
}

Upvotes: 0

Tim.Tang
Tim.Tang

Reputation: 3188

phone number 's length is 4-9?

(?<=\[[a-zA-Z]+\s)\d{4,9}(?=\d*\])

DEMO

Upvotes: 1

Related Questions