Regex Workout 006 - Find names

Welcome in next Regex workout. If you are new to this, please see 001 edition with manual.

Difficulty level : 4 stars

Today we will try to find some names in sentences.

Task 1.

Find the marked names.

Source: Steven and Stephen bought a Stevensons&Stephensons brand stereo set from their stepfather Steve

Task 2

Find starwars robots in sentences. There are no clear rules, think what they have in common.

Source: R2D2 and C3PO are the most famous robots in space.GeForceRTX4090 is not a robot.

Task 3

Find names in sentences without “gun” word

Source: "Harry bought gun
Will bought gun and mobile
Tom bough computer
Lola bought dog
"

I will publish solutions on 20230625. Good luck;)

1 Like
Task1

S\w+n\b

Task2

\b[A-Z|0-9]+\b

Task3

^\w+\b(?!.*gun)

1 Like

Hi @KrzysztofNowak,

Here is my solution to this workout:

Task 1:

Task 2:

Task 3:

Thanks to Chapgpt for the help on this too. I’m learning alot reading different ways of finding things.

Thanks
Keith

1 Like

@KrzysztofNowak,

Task 1

Find names in “Steven and Stephen bought a Stevensons&Stephensons brand stereo set from their stepfather Steve”

I used /(?<!&)\b[A-Z][a-z]*(?:\s+[A-Z][a-z]*)*(?!\S)/g to capture any name unless separated by an ampersand.

An alternative that only captures “Steven” and “Stephen” but not “Stevensons&Stephensons” or “Steve” is:
/Ste(?:v|ph)en(?!\S)/gi

Task 2

This regular expression, /\b[A-Z0-9]+\b/g matches robot names that comprise only capital letters or numerals.

Task 3

To find names in sentences without the word “gun” in the line, this regular expression works: /^(?!.*gun)([A-Z][a-z]+)/gm

1 Like

Thank You for participation by far @Keith , @HufferD , @borydobon

Please see my solutions: