Dynatrace用户会话标签清理:提取SharePoint用户名正则需求
Got it, let's build a regex that handles both your SharePoint header formats and outputs the name in "First Name Last Name" order for your Dynatrace user session tags.
The Regex & Replacement Logic
Use this regular expression to match the username segment, then swap the captured groups to reorder the name:
Match Pattern:
^([^,]+),\s*([^\s\n]+)(?:\s|\n)
Replacement String:
$2 $1
How It Works
Let's break down each part of the match pattern to make sure it fits your use case:
^: Anchors the match to the start of the text (ensures we're grabbing the very first username segment)([^,]+): Captures the last name (everything up to the comma) into Group 1,\s*: Matches the comma followed by any number of whitespace characters (handles cases where there's a space after the comma, or none)([^\s\n]+): Captures the first name (everything up to the first space or newline) into Group 2(?:\s|\n): Matches either a space or a newline (the separator between the username and the prompt text) — the(?:...)makes this a non-capturing group so it doesn't interfere with our name groups
Testing with Your Examples
Let's verify it works for both your input formats:
Example 1: Space Separator
Input:
Smith, John Use SHIFT+ENTER to open the menu (new window).
- Group 1 captures
Smith, Group 2 capturesJohn - Replacement gives:
John Smith
Example 2: Newline Separator
Input:
Smith, John Use SHIFT+ENTER to open the menu (new window).
- Group 1 captures
Smith, Group 2 capturesJohn - Replacement gives:
John Smith
Bonus: Making It Even More Flexible
If you want to ignore any trailing prompt text entirely (not just match up to the space/newline), you can adjust the pattern to:
^([^,]+),\s*([^\s\n]+)
This will just grab the last-name-first segment regardless of what comes after, which might be useful if the prompt text ever changes slightly.
内容的提问来源于stack exchange,提问作者Davide Piras

