Home > other >  Snowflake Regex for middle name
Snowflake Regex for middle name

Time:07-26

I wouldn't post a regex expression normally, but my expression works on https://regex101.com/ but not within Snowflake.

I have values like lastname, firstname, lastname, firstname middlename, last-name, firstname, last name, firstname

and I need to extract just first name everytime.

My current expression is:
regexp_substr(name, '.*,(.*)( *| $)',1,1,'e')
I was hoping this would grab everything after the first name before either a line break or end. I also tried with \W instead of \b

CodePudding user response:

You can use

regexp_substr(name, '.*, *(\w )', 1, 1, 'e', 1)

That is,

  • search for zero or more chars as many as possible, then a comma, then zero or more spaces, and then capture one or more word chars into Group 1 (.*, *(\w ))...
  • from the first char in the string (the first 1)...
  • and get the first match (the second 1)...
  • and only return the Group 1 value ('e', 1).

CodePudding user response:

Got it working with this:
.*, (\\w )',1,1,'e

Just so someone can search this, this finds everything following a comma and a space that is a word character.

  • Related