/    Sign up×
Community /Pin to ProfileBookmark

regex first and last name

I’ve never got my head around these regex things.

I’m looking to match firstname-lastname

I know (A-Za-z) will match the names without the hyphen between them but I have a hyphen .

I also need to cater for comma and hyphen within the names, as well as between their first and last name.

I don’t need to separate them, just find this pattern.

Any help please

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@kiwisauthorFeb 13.2022 — This is where I'm at. It works expect for the comma


^([A-Za-z]*-+)
Copy linkTweet thisAlerts:
@NogDogFeb 13.2022 — Difficult to give a good answer without understanding the context. For example, /b[a-z,-]+b/i ("/i" for case-insensitive) would match (I think), but it might match other non-name things as well. If a hyphen somewhere in the middle is required, that might make it a bit more specific:
<i>
</i>/b[a-z,]+-[a-z,-]+b/i

For now I'm using "b" word-**b**oundary indicators rather than locking it to the start of the string with "^", but of course if in fact it's always at the start of the string, you could change it back to that.
Copy linkTweet thisAlerts:
@kiwisauthorFeb 14.2022 — @NogDog#1642444

I have a URL it's like

mypage.com/123/Nog-Dog

I can get the 123 from the link, But trying to get the second part. Your name might be Nog's-Dog or Nog-Nog-Dog

It's a pain, it's not my URL's but trying to redirect it
Copy linkTweet thisAlerts:
@NogDogFeb 14.2022 — Doesn't sound like you need to do any filtering, instead just grabbing whatever is there, maybe like...
<i>
</i>^([0-9]+)/(.+)

$1 would then have "123" and $2 would have "Nog-Dog"
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...