TypeScript get specific string combinations for array of strings - combinations

I'm trying to create a name-twister to get the correct format and combination "surname, givenName" for my api....
You should be able to put in a string array, for example:
["John", "Paul", "Stirling"])
and it should return all string combinations that fit into the schema:
John, Paul Stirling
John, Stirling Paul
Paul, John Stirling
Paul, Stirling John
Stirling, John Paul
Stirling, Paul John
I was thinking about working with a sorting algorithm. The problem is that there is no fixed number of names entered. The user can also enter only "John" and "Paul" or maybe even 4 names.
I would highly appreciate, if someone would come up with a solution idea!! Thanks in advance!

Related

Regex return all text other than text in MULTIPLE sets of parentheses

I am looking for regex that will allow me to extract the names and drop everything inside the parentheses. Example data below.
Text string:
John (Juan, Jonathan, Jon, Jonny) James Doe (born on January 1, 1900)
Desired output:
John James Doe
Further in some cases the text string may be like:
John (Juan, Jonathan, Jon, Jonny) James Doe (born on January 1, 1900) (Canada)
and in this case we would still want returned:
John James Doe
I tried the solution from the linked question, but I still get the wrong output:
John James Doe (born on January 1, 1900)
Using regex only and not any replace function :
[^\S]*(\w*)(?:\s*)(?:\([^()]*\))*
https://regex101.com/r/zF2cMM/4
Edit :
(?:[^\S]*(\w*)(?:\s*)(?:\s*\([^()]*\)\S*)*)
I have made this last one version correcting a problem on last match
You can compare V4 and V6 and see that the result is a little bit different
https://regex101.com/r/zF2cMM/6
Now this work fine.

Regex - Creating validation to enforce that a string has 2+ words

If you have a moment, I need some help adding to my regex expression. I am validating a response in a Google Form for the user's full name.
The validation requires:
That only letters are used
That the user inputs both the first and second name (at a minimum), separated by a space
So far I have come up with:
[a-zA-Z ]+]
But this lacks the check for a minimum of two words in a given string.
After an hour of fails and googling, I have admitted defeat and need your help!
Thanks in advance.
This should do the job:
/^[a-z]{2,}( [a-z]+)*?( [a-z]{2,}){1,}$/i
It matches:
john smith ◄ all lowercase
John Smith
John P E Smith
John Paul E Smith
John Paul Eward Smith
It ignores:
John
John S
John Paul S
John Paul Edward S
J0hn Smith  ◄ zero instead of the letter 'o'
John     Smith  ◄ multiple spaces
You can play with this fiddle.
Best regards

How to extract multiple names with capital letters in Google Sheets?

I am trying to extract contact names of a data set, however, the names are compiled in one cell and not split up by first name, middle name, last name, email, etc.
I only need to get their names because I already have a data set only with their emails, NOT their names.
How do I extract multiple case-sensitive words and split into cells?
Here's how it looks like in one cell:
I've tried several codes I've found online and this is the only thing that comes close, however, it still extracts unnecessary lower case letters which I don't need. Please help, I'm no expert with these kinds of things.
=TRANSPOSE(SPLIT(TRIM(SUBSTITUTE(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(
A2,"\b\w[^A-z]*\b"," "),"\W+"," "),"[0-9]+","")," m "," "))," "))
I expect them to have the first, middle, last names to be split into new columns like this:
Tom Billy Claudia Downey Karen Dicky Steve Harvey
OR
Tom Billy Claudia Downey Karen Dicky Steve Harvey
=ARRAYFORMULA(TRIM(IFERROR(REGEXREPLACE(IFERROR(REGEXEXTRACT(IFERROR(SPLIT(A2:A,
CHAR(10))), "(.*) .*#")), "Mr. |Mrs. ", ""))))
This formula might help. i have added the conditions to replace the email id and Mr./Ms. conditions.
=TRANSPOSE(SPLIT(TRIM(SUBSTITUTE(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(
REGEXREPLACE(REGEXREPLACE(A2,"([a-zA-Z0-9_\-\.]+)#([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]
{2,5})",""),"\w+[\\.]+(?)",""),"\b\w[^A-z]*\b"," "),"\W+"," "),"[0-9]+","")," m ","
"))," "))

Not sure how to return a phrase after a match, and also include a conditional clause

I'm new to regular expressions, and I've spent the last two days working through online tutorials. I think I understand some of the basics, but I'm at a loss trying to implement it with some data I have.
I have a few phrases:
Hello and welcome. My name is Amanda, how may I assist you today?
Hello and welcome. My name is Daniel L, how may I assist you today?
Hello and welcome. My name is John Livingston , how may I assist you today?
Hello, my name is Alyssa D.. How can I help?
Hello, my name is John. How can I help?
Hello and welcome. My name is Felicia F., how may I assist you today?
Hello and welcome. My name is Alex how may I assist you today?
Hello and welcome. My name is Alex P how may I assist you today?
I'm trying to retrieve the name that comes after some variation of "my name is".
The problem is, sometimes there is a first name, name with initial, or first and last name. Sometimes a period follows the name, sometime a comma, and sometimes nothing at all (except the word 'how', which could be the criteria).
How do I return just the full name?
Right now I have:
[Mm][Yy]\s*[Nn][Aa][Mm][Ee]\s*[Ii][Ss]\s*(\w+\s?\w*)
But it doesn't solve the problem
1. It matches the whole phrase, not just the name
2. In the case of 'Alex', it returns 'Alex How'
Any help would be appreciated. I can't seem to make any progress on it! Oh, I'm using R if that helps.
This uses strapplyc in the gsubfn package which extracts the capture group (the part matching the parenthesized portion of the regular expression).
# test data
Lines <- "Hello and welcome. My name is Amanda, how may I assist you today?
Hello and welcome. My name is Daniel L, how may I assist you today?
Hello and welcome. My name is John Livingston , how may I assist you today?
Hello, my name is Alyssa D.. How can I help?
Hello, my name is John. How can I help?
Hello and welcome. My name is Felicia F., how may I assist you today?
Hello and welcome. My name is Alex how may I assist you today?
Hello and welcome. My name is Alex P how may I assist you today?"
L <- readLines(textConnection(Lines))
library(gsubfn)
strapply(L, "is ([A-Z][a-z]*( [A-Z][a-z]*[.]?)?)", simplify = TRUE)
giving:
[1] "Amanda" "Daniel L" "John Livingston" "Alyssa D."
[5] "John" "Felicia F." "Alex" "Alex P"
Here is a visualization of the regular expression:
is ([A-Z][a-z]*( [A-Z][a-z]*[.]?)?)
Debuggex Demo

Regex get next 2 words after certain string

I need a regular expression, which can find names in some text content. It should match from 1 to 3 names, First-name, (Middle-name), (Surname).
I have a list of valid first-names which will be used to search the text. If the first-name is found in the text, the regular expression should get the next middle-name or/and surname, if they exists.
As an example the names below, should be valid names found:
John
John Doe
John Average Joe
Special cases:
John average Doe (if, possible it should match/find John Doe)
So far my solution is:
\b(John|Mary|Tom)\b(?:(?:([^A-Za-z]*[A-Z][^\s,]*)*[^A-Za-z]+)){0,3}
This kinda works, the problem is the limitation to only match maximum 3 words, which this doesn't.
Online test: http://regex101.com/r/aM7bS3/2
I've modified your regex HERE
You can use the following:
\b(Mogens|Victor|John)(\b\s*([A-Z]\w+)){0,2}