0

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>[email protected]<kt
z>[email protected]<kt
z>[email protected]<kt
z>[email protected]<kt

After applying regular expression my emails should look like:

[email protected]
[email protected]
[email protected]
[email protected]

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

1
  • 2
    These are good examples of what you want done. Can you tell us what you have tried as well? Commented Nov 6, 2013 at 16:47

4 Answers 4

3

You can try deleting matches of

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

(demo)


Regular expression visualization

Debuggex Demo

0
0

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

Here's an example on regex101

0

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: [a-z][A-Z][0-9]*.?[a-z][A-Z][0-9]+@[a-z][A-Z][0-9]*.[a-z]+

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.
0

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)

Not the answer you're looking for? Browse other questions tagged or ask your own question.