2

I'm using a regex pattern to extract matching items from a string

My pattern is :

"^(([^\\.\\-\\,a-wy-z]([\\(]?(\\+|[x])?\\d+[\\)]?)?[\\s\\.\\-\\,]?([\\(\\d[\\)]?)?[\\s\\.\\-\\,]?(\\d+[\\s\\.\\-\\,]?)+[^\\.\\-\\,a-z])|((\\+|[x])?\\d+))$"

My string is

"This is the string +971 987654321 form which +91 9876543210 all the phone number +91 987 654 3210 has to be extracted +91 987 654 3210 and displayed in the +971-98-7654321 logcat and the post office box  number is 233227"

But unfortunately the pattern is not identifying any matching pattern from the string. I have validated the expression from "Check RegExp" through intention action. Here the pattern successfully identifies all the phone numbers that i have used in my string

Any help is appreciated.

2
  • RegEx is considered a write only language for a reason. That's a nearly impossible to read piece of code. Regex on english strings, especially when trying to find something as ill formatted as a phone number, is generally not a good approach. Commented Apr 22, 2016 at 6:03
  • Assuming least number of 10 digits in phone number :- regex101.com/r/iO0dI3/1 will work Commented Apr 22, 2016 at 6:05

1 Answer 1

1

Assuming at least 10 digits in your phone number(excluding +, space, - etc.), this will work

\s+(\+[\d\s-]{10,})\s+

Regex Demo

If you assume, there can be no spaces separating a number and word,then you can use

(\+[\d\s-]{10,})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot @rock321987. That one really helped me

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.