I am needing to grab the last name from a string and have it update the Jira field. I have this working for the first name but not sure what the regex should be for the last name rule.
Here is the string:
The following records request has been submitted by Bryan Trummer :
I am just needing to {{issue.description.match}} the bold (obviously this will be variable as it's a last name).
Not sure what the regex should be as I know how to get it to skip over everything but the first name.
Ah Regex. Do you have a license?
I'm kidding of course, but RegExes are notoriously brittle.
I went over to a handy Regular Expressions Tester and came up with this
{{issue.description.match("submitted by .+ (.+) :")}}
And that seemed to work fine.
HOWEVER. It won't work for names like:
Oh, and it would also fail in the unlikely event that somebody's name was:
So, as always with Regexes, you want to think about edge cases. You could certainly add additional code to handle Jr. (and Sr.! both with and without periods!), and II, III, IV... and even Esq. and all the possible degrees somebody might have.
But yeah. It all depends on the source of the tickets. If they're being generated by something that is always sending:
The following records request has been submitted by FIRSTNAME LASTNAME :
Then this ought to work.
This worked like a charm! I thought I was over engineering it with my regex and it turns out I was. Thank you @Darryl Lee
I did switch it up just a little to account for those additional areas in the last name
{{issue.description.match("submitted by .+ ([a-zA-Z'-_]+) :")}}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Bryan Trummer - ReleaseTEAM - glad that worked.
But about your change. If you were trying to account for my edge cases, it won't work for cases like "Jr." or "Esq." because we are still trying to match on that final " :"
But also, according to the regex tester, the section '-_ is getting interpreted as a "range of characters between ' and _". If you want to match the hyphen, you need to escape it with a backslash, so then:
{{issue.description.match("submitted by .+ ([a-zA-Z'\-_]+) :")}}
So when we test that, you can see that it works for "regular" names, but doesn't really help with Jr. or Esq. and worse, instead of including the "wrong" last name, doesn't match at all.
https://regex101.com/r/43Isyq/1
What I think you were expecting is it to do is "ignore" any words that end in a period. That's trickier. I tried this:
submitted by .+ ([a-zA-Z'\-_]+)( [a-zA-Z'\-_]+\.)? :
But as you can see from the test results below, this fails to pick up our friend William because his last name includes a comma.
https://regex101.com/r/rr1Czr/1
You can really overcomplicate things trying to account for all edge cases. I do not recommend this. A good old (.+) will work 99% of the time, and like I said, if whatever is creating the tickets is using FIRSTNAME LASTNAME, then this should not be an issue.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
OH, I found this great recipe that demonstrates why this is SO HARD:
4.18. Reformat Names From “FirstName LastName” to “LastName, FirstName”
I tried to fit this into your use case:
submitted by (.+?) ([^\s,]+)(,? (?:[JS]r\.?|III?|IV))? :
And it works similarly to my take, and also fails on Esq., but you could certainly add it to the possibilities.
Ohhhh K, even though I just said you shouldn't try to do complex things like this, but I couldn't resist:
https://regex101.com/r/8OUOPm/1
submitted by .+? ([^\s,]+)(?:,? (?:[JS]r\.?|Esq\.|III?|IV))? :
(God I love this tool. Thank goodness that the tester has great hover-over explainer tooltips to tell me that ?: creates a non-capturing group, because I did not want to have to deal with grabbing a particular match.)
So yeah I just did some tests with Automation and it passed all the tests.
I guess the good thing is that the rule is a little easier to read and more explicit.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.