John Doe
John Doe

Reputation: 51

Messy string. Regex for excluding '@' char in the end

I have a pretty much messy array of strings which doesn't follow any particular pattern.

Basically, it's users' properties which are messed up (all info in one string without following any kind of pattern).

I'm interested in 2 particular properties (email and number). I found a way around to get email and thought that the following regex:

^9[0-9]{9}

would work for users' phones. However, some users do have emails which are equal to phone numbers + '@'.something. That seems to be a problem.

So, I need a regex which will exclude the following and give me just a number.

9876548877@

I've tried

^9[0-9]{9}((?!@).{0})*$"

And get full match here:

9876548877

But it works so well only if the string doesn't contain anything apart from this.

I am trying to achieve is getting exactly phone number in a string like this:

/* mess mess mess*/ John Doe Jr email: [email protected], phone number: 9876548877, /* more mess */

How do I do it? Thanks in advance.

UPD:

Thank you for your answers sirs, but what the task is a little bit different

For example, I took a regex from here and then I test it here I get the result I want. I'm trying to accomplish the same behaviour, but with the phone number and without '@' to be sure that it's exactly what I'm looking for.

The question wasn't described properly. My bad.

Upvotes: 4

Views: 159

Answers (1)

The fourth bird
The fourth bird

Reputation: 163362

You could use lookarounds to assert what is on the left is not a non whitespace char and on the right in not an @:

(?<!\S)9[0-9]{9}(?!\@)

Regex demo

If there can be for example a : directly before the number you could omit the lookbehind at the start and use a word boundary \b

Upvotes: 1

Related Questions