简体   繁体   中英

Regular expression to exclude particular pattern match

I need a regular expression for matching telephone no but not " [1398340387] " --[10digits] pattern

I have this regular expression to identify phone no but this is also matching [1398340387] which i want to escape.

1?\s*\W?\s*(\d{3})\s*\W?\s*(\d{3})\s*\W?\s*(\d{4})(\se?x?t?(\d*))?

Context:

I have a string that can possibly contain phone no(s) and [1398340387]: unix time stamp and i need to highlight all the phone nos if found. Right now Unix time stamp is also identified as phone no and highlighted and I need to avoid this.

Sample string

[1398337818] * Conversation started * with 'yash' [1398337818] sam: Telephone no : 1-234-567-8901 1-234-567-8901 x1234 1-234-567-8901 ext1234 1 (234) 567-8901 1.234.567.8901 1/234/567/8901 12345678901 [1398337818] sam: hello [1398337905] sam: credit card 1111-2222-3333-4444 [1398338242]

What should i add to escape the later part?

Thanks

The following regex will match only phones in your sample:

\b1\s*[-/\.]?\(?\d{3}\)?\s*[-/\.]?\d{3}[-/\.]?\d{4}(\s+(x|ext)\d{4})?\b

正则表达式可视化

Debuggex Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM