Trimming a line with a space

I would like some help to handle someones name string. I would like to take a string and delete it so that only the first name is in the string.

Suppose I have a name like this

Mr. John Doe
John Smith.

In both cases, I would like to get only the first line name and delete all other characters.

So, for both lines after they have been analyzed, will have Johnin them

I was wondering if there is a way to make this problem with regex.

+3
source share
5 answers

. , . John Paul Doe ( "John", "Paul", "Doe" ), John Joseph Brown ( "Joseph" "Joe", - "John" on ), ( " " ).

Go Falsenesss .

, , , 95% , . (80%, ).

, , , , "" , "Mr", - ( "Mr", , -, ).

s/^\s+//; s/\s+$//;     # trim whitespace at each end
s((\s+))(               # trim embedded whitespace
    $1=~/[^\x{a0}]/ ?   # breakable?
    " " : "\x{a0}")ge;
+6

, $1.

^(?:Mr\.|Mrs\.)?\s*\b([^\s]*)\b.*$

, Regexr

+1

,

/^(?:\w+\.)?\s*(\w+).*$/
// $1 = John

:
\w+\. , ( )
(\w+\.)? (?:\w+\.)? ( )
^(?:\w+\.)? ^ ( ) ^(?:\w+\.)?\s* ( none)
^(?:\w+\.)?\s*(\w+), ( ) ^(?:\w+\.)?\s*(\w+).*$ finally .* $

+1

?

, :

/(?<=((Mr\.|Mrs\.)\s+)?)([a-zA-Z]+)/
0

,

my $nameFull = 'Mr. John Doe';
my $nameFirst = $1 if $nameFull =~ /(?:\s|^)(?!(?:mr|mr?s|miss|dr|prof)(?![a-z]))([a-z]+)/i;

:

/... /iThe beginning and end of a case-insensitive regular expression

  • (?:\s|^) Make sure that we either find the white space or the beginning of the line.
  • (?!... make )sure that this does not match the beginning of the first name
    • (?:mr|mr?s|miss|dr|prof)List of abbreviations ( r?means optional r, so this will match Msand Mrs)
    • (?![a-z])Make sure that there are no more letters after the abbreviation, because drake- this is the name that begins withdr
  • (... )grab it up$1
    • [a-z]+How many letters are in a line. Suppose at least one.
0
source

All Articles