2

I have this Regex

(?<=\\).*$

I string is domain\username and I like to get the username. This regex code only gives me username without the domain.

What's the code to get forward value after the first backslash "\". The regex should give me username.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Fuji - H2O
  • 367
  • 1
  • 5
  • 17
  • `var userName = yourText.Split('\\')[1];` – Robert McKee Dec 05 '16 at 20:34
  • It has to be regex. I can't declare variables in sharepoint workflow but it will take .net friendly regex one liner code. – Fuji - H2O Dec 05 '16 at 20:35
  • Your question is unclear: please clearly type the output you are getting and the output you are expecting. "...only gives me domain with the backslash (domain)" doesn't really explain what you are expecting, and you shouldn't be getting the backslash. – leigero Dec 05 '16 at 20:37
  • Sorry Leigeo: I meant I am needing to get the username not the domain. – Fuji - H2O Dec 05 '16 at 20:41
  • 1
    The regex is correct, but it depends on what the code is doing with the match. `(?<=\\).*$` should return `user` from `domain\user` Are you throwing it into a split (or replace) function instead of one that returns the match/value? – Robert McKee Dec 05 '16 at 20:43
  • [Your regex works well](http://regexstorm.net/tester?p=%28%3f%3c%3d%5c%5c%29.*%24&i=domain%5cuser) if you *extract* the value. To use a replacing approach, use [``^.*\\``](http://regexstorm.net/tester?p=%5e.*%5c%5c&i=domain%5cuser) – Wiktor Stribiżew Dec 05 '16 at 20:43
  • Thanks Wiktor. The Regex is working when I select Extract in the workflow action. If I select "Replace" then it returns the \domain. I think it will do the job because it will put the user name in the new workflow variable. thanks everyone – Fuji - H2O Dec 05 '16 at 20:47
  • Are you using Nintex? – Wiktor Stribiżew Dec 05 '16 at 20:53
  • 1
    Hi Wiktor, I am using Nintex workflow for SharePoint. and your proposed solution ^.*\\ worked for me. Can you reply as an answer so I can accept your solution? Thank you SOOOo much for your help. – Fuji - H2O Dec 05 '16 at 21:18
  • Please do not forget to put `@` before the user name so that users could be notified of your feedback. – Wiktor Stribiżew Dec 05 '16 at 21:51

1 Answers1

1

In a Nintex workflow, there are several types of actions.

If you select Extract, the [^\\]+$ should fetch you all chars other than \ at the end of the string (thus, fetching b if a\b is given).

If you use Replace, the ^.*\\ will match and remove any 0+ chars other than newline (in .NET, a newline) as many as possible up to the last \ and the backslash itself, turning x\b into b.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563