Say I have the string ‘User Name:firstname.surname’ contained in a larger string how can I use a regular expression to just get the firstname.surname part?
Every method i have tried returns the string ‘User Name:firstname.surname’ then I have to do a string replace on ‘User Name:’ to an empty string.
Could back references be of use here?
Edit:
The longer string could contain ‘Account Name: firstname.surname’ hence why I want to match the ‘User Name:’ part of the string aswell to just get that value.
I like to use named groups:
Putting the
?<something>at the beginning of a group in parentheses (e.g.(?<something>...)) allows you to get the value from the match usingsomethingas a key (e.g. fromm.Groups['something'].Value)If you didn’t want to go to the trouble of naming your groups, you could say
and just get the first thing that matches. (Note that the first parenthesized group is at index
1; the whole expression that matches is at index0)