简体   繁体   中英

Regular expression to correct email address

I need help in writing one regular expression where I want to remove unwanted characters in the start and end of the email address. For example:

z>user1@hotmail.com<kt
z>user2@hotmail.pk<kt
z>puser3@yahoo.com<kt
z>npuser4@yaoo.uk<kt

After applying regular expression my emails should look like:

user1@hotmail.com
user2@hotmail.pk
puser3@yahoo.com
npuser4@yaoo.uk

Regular expression should not applied if email address is already correct.

You can try deleting matches of

^[^>]*>|<[^>]*$

( demo )


正则表达式可视化

Debuggex Demo

Find ^[^>]*>([^<]*)<*.*$ and replace it with \\1

Here's an example on regex101

Try using a capturing group on anything between the characters you don't want. For example,

/>([\w|\d]+@[\w\d]+.\w+)</

Basically, any part that the regexp inside () matches is saved in a capturing group. This one matches anything that's inside >here< that starts with a bunch of characters or digits, has an @, has one or more word or digit characters, then a period, then some word characters. Should match any valid email address.

If you need characters besides >< to be matched, make a character class . That's what those square bracketed bits are. If you replace > with [.,></?;:'"] it'll match any of those characters.

Demo (Look at the match groups)

I think you might be missing the point of a regular expression slightly. A regular expression defines the 'shape' of a string and return whether or not the string conforms to that shape. A simple expression for an email address might be something like: [az][AZ][0-9]*.?[az][AZ][0-9]+@[az][AZ][0-9]*.[az]+

But it is not simple to write one catch-all regular expression for an email address. Really, what you need to do to check it properly is:

  1. Ensure there is one and only one '@'-sign.

  2. Check that the part before the at sign conforms to a regular expression for this part:

    • Characters
    • Digits
    • Extended characters: .-'_ (that list may not be complete)
  3. Check that the part after the @-sign conforms to the reg-ex for domain names:
    • Characters
    • Digits
    • Extended characters: . -
    • Must start with character or digit and must end with a proper domain name ending.

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