0

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

3
  • 3
    Have you searched for "regex number in brackets"? Commented Aug 19, 2014 at 8:35
  • Have you tried anything yourself? There are plenty of regex tutorials out there. To get you started, you could start by trying to devise a regex to return the text between the square brackets. Commented Aug 19, 2014 at 8:36
  • I do not need all the numbers in brackets. now i use (\d{4,})[^(9902])] but on short numbers is wrong Commented Aug 19, 2014 at 8:38

4 Answers 4

1

phone number 's length is 4-9?

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

DEMO

Sign up to request clarification or add additional context in comments.

Comments

0

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

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

DEMO

OR

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

DEMO

2 Comments

just reminder: . will match not only number ,but also other non-newline , do you think it is good for phone number?
@Tim.Tang is this (?<=[A-Z] )\d+?(?=9902|]) ok for you?
0

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);
}

1 Comment

Just to note that the escaping slashes in your first and last character classes are unnecessary. [ has no special meaning in a character class, and ] does not need escaping if it is the first character in the class.
0

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

6 Comments

[ is keyword in Regex for C#, so we need to escape it as \[ first
I don't think so as per the testing site.
Try deleting the opening [, or the letter, from the input, not the pattern, and you'll still get a match. You need to escape the first [ in the pattern.
@Rawling OK updated but don't know why regex sites doesn't claim for it
I also tried it on the regex site. Just because your pattern matches the input doesn't mean it doesn't match other, incorrect input.
|

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.