I have to match
Salutation(optional) FirstName LastName
My regex is:
^(([mMrRsSdDlLtTcCoO]{2,4})\.?\s+)?([a-zA-Z,.'-]{2,}\s+)([a-zA-Z,.'-]{2,})$
The problem with my regex is it works fine with
Mrs. Pamela Anderson or Pamela Anderson
as well on Miss Pamela (wrong case).
Your regex is matching Miss as FirstName and Pamela as Last name
your regex should be
^(?!(Mr|Miss|..)[.]?\s+[a-zA-Z,.'-]+$)[a-zA-Z,.'-]+[.]?(\s+[a-zA-Z,.'-]){1,2}$
-------------------------------------
|
|->don't match further if it has Salutation and firstname or lastname
As a side note,don't use [mMrRsSdDlLtTcCoO]{2,4} there can be many permutations of valid names like coco(my friends name),Rod
Specify it explicitly using | like (Mr|Mrs|Miss|Dr)
Try this:
(?:^[mMrRsSdDlLtTcCoO]{2,4}\.?\s+?)?([\w,.'-]{2,})\s+([\w,.'-]{2,})
Related
I'm trying to use Notepadd ++ to find and replace regex to extract names from MS Outlook formatted meeting attendee details.
I copy and pasted the attendee details and got names like.
Fred Jones <Fred.Jones#example.org.au>; Bob Smith <Bob.Smith#example.org.au>; Jill Hartmann <Jill.Hartmann#example.org.au>;
I'm trying to wind up with
Fred Jones; Bob Smith; Jill Hartmann;
I've tried a number of permutations of
\B<.*>; \B
on Regex 101.
Regex is greedy, <.*> matches from the first < to the last > in one fell swoop. You want to say "any character which is neither of these" instead of just "any character".
*<[^<>]*>
The single space and asterisk before the main expression consumes any spaces before the match. Replace these matches with nothing and you will be left with just the names, like in your example.
This is a very common FAQ.
I have the following input:
Text1 FirstName LastName (10) Text2
I need to fetch the full name without the parenthesis. For example:
User: John Doe (10) Email:
Result: John Doe
Thanks in advance for the help!
Try using this regex on the line containing the first and last name:
^(.*)\s\(\d+\)$
Regex101
To match just the target you're after, use a look arounds (which don't capture):
^(?<=User: \n).*(?=\s+\(\d+\)\s*$)
The entire match will be "John Doe".
See live demo.
Let's say I have a string:
John Smith (auth.), Mary Smith, Richard Smith (eds.), Richie Jack (ed.), Jack Johnny (eds.)
I would like to match:
John Smith(auth.),Mary Smith,Richard Smith(eds.),Richie Jack(ed.),Jack Johnny(eds.)
I have came up with a regex but I have a problem with the | (or character) because my string contains characters that have to be escaped like ().. This is what I'm not able deal with. My regex is:
\s+\((auth\.\)|\(eds\.\))?,\s+
EDIT: I think now that the most universal solution would be to assume that in () could be anything.
Try this:
\s*\((auth|eds?)?\.\)?,?\s*
\s+ means one or more
\s* means zero or more
Based on your comment, I modified the regex:
\s*((\([^)]*\))|,)\s*
What would be the regular expressions to extract the name and email from strings like these?
johndoe#example.com
John <johndoe#example.com>
John Doe <johndoe#example.com>
"John Doe" <johndoe#example.com>
It can be assumed that the email is valid. The name will be separated by the email by a single space, and might be quoted.
The expected results are:
johndoe#example.com
Name: nil
Email: johndoe#example.com
John <johndoe#example.com>
Name: John
Email: johndoe#example.com
John Doe <johndoe#example.com>
Name: John Doe
Email: johndoe#example.com
"John Doe" <johndoe#example.com>
Name: John Doe
Email: johndoe#example.com
This is my progress so far:
(("?(.*)"?)\s)?(<?(.*#.*)>?)
(which can be tested here: http://regexr.com/?337i5)
The following regex appears to work on all inputs and uses only two capturing groups:
(?:"?([^"]*)"?\s)?(?:<?(.+#[^>]+)>?)
http://regex101.com/r/dR8hL3
Thanks to #RohitJain and #burning_LEGION for introducing the idea of non-capturing groups and character exclusion respectively.
use this regex "?([^"]*)"?\s*([^\s]+#.+)
group 1 contains name
group 2 contains email
(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))
https://regex101.com/r/pVV5TI/1
You can try this (same code as yours but improved), but you need to check returned groups after matching because the email is either returned in group 2 or group 3, depending on whether a name is given.
(?:("?(?:.*)"?)\s)?<(.*#.*)>|(.*#.*)
This way you can get with or without name, removing the quotes.
\"*?(([\p{L}0-9-_ ]+)\"?)*?\b\ *<?([a-z0-9-_\.]+#[a-z0-9-_\.]+\.[a-z]+)>?
Although #hpique has a good answer, that solution only works when the name/email string is the only thing being analyzed in Regex. It will not work when you have a longer message that contains other items, such as an email. Also many of the other solutions will fail to match when the person has included a middle name (i.e. James Herbert Bond <jbond#example.com).
Here is a more robust Regex solution I wrote that can pick up the first names, last names, and emails like you wanted, even if there are many other things in the string:
/(?:"?)(\b[A-Z][a-z]+\b ?)(\b[A-Z][a-z]+\b ?)*(?:"?) ?<([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)>|([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/g
Check out the above syntax here: Example on Regexr
regexes?
to validate a name with only characters and spaces e.g.
Jon Skeet
to validate a number having digits and dashes anywhere e.g.
423-4324234-423
4233-412341324
A basic english name:
([a-zA-Z]+\s*)+
Numbers with dashes anywhere except for beginning and end:
\d[-\d]+\d
Numbers with dashes anywhere:
[-\d]+
Edit:
If you are looking for name inside of a sentence, such as
Hello, my name is John Doe.
You can try and capture names based on two or more capitalized words in a row.
([A-Z][A-Za-z]+\s*){2,}
Jon Skeet
/([a-zA-Z ]+)/
423-4324234-423
4233-412341324
/([\d-]+)/
To match a name in any language :
/^[\p{L} '-]+$/
That matches O'Connors Anne-Marie El NiƱo ...
to match your example digits:
/^[\d-]+$/