locked
Return all the strings that are in the format '[Company Name]\username' from string array. RRS feed

  • Question

  • How can I find strings that are in the format [Company Name]\username in a string array?

    I think I need to use Regex but can't seem to find a way to work.

    Wednesday, May 8, 2019 8:24 PM

Answers

  • I'd modify that regex. Besides the typo (you have a '-' instead of a '.' in the first group), it matches some 'non-optimal' conditions such as an empty string where a company name would be expected, and an empty string where a use name would be expected. I'd also use a non-greedy modifier in the first group (just to avoid unnecessary backtracking.

    This one should work better:

    '(\[.+?\])\\(.+)'

    Erring on the side of caution, he might want to add anchors to the regex, too (it hardly ever hurts and avoids matching bad data). I'd probably change the match in the last group to use '\w' instead of '.', too (unless the use name contains 'odd' characters):

    '^(\[.+?\])\\(\w+)$'


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, May 8, 2019 9:31 PM

All replies

  • $str -match ' (\[.*\])\\(.*)
    $matches


    \_(ツ)_/



    Wednesday, May 8, 2019 9:01 PM
  • I'd modify that regex. Besides the typo (you have a '-' instead of a '.' in the first group), it matches some 'non-optimal' conditions such as an empty string where a company name would be expected, and an empty string where a use name would be expected. I'd also use a non-greedy modifier in the first group (just to avoid unnecessary backtracking.

    This one should work better:

    '(\[.+?\])\\(.+)'

    Erring on the side of caution, he might want to add anchors to the regex, too (it hardly ever hurts and avoids matching bad data). I'd probably change the match in the last group to use '\w' instead of '.', too (unless the use name contains 'odd' characters):

    '^(\[.+?\])\\(\w+)$'


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, May 8, 2019 9:31 PM
  • How does a dot get to be a dash?  That is quite odd.


    \_(ツ)_/

    Wednesday, May 8, 2019 9:33 PM
  • Thank you jrv, but what if I only want to find those user whose name starts with 'sh' or 'ja'

    [Company]\shawn, [Company]\shaila, [Company]\jason?

    Wednesday, May 8, 2019 10:28 PM
  • Thank you jrv, but what if I only want to find those user whose name starts with 'sh' or 'ja'

    [Company]\shawn, [Company]\shaila, [Company]\jason?

    We call this scope creep.  You asked a specific question and now you are adding new requirements without even using the requested solution.  We cannot design and write your code one request at a time.

    You will have to take some time to learn the technology that you need to use.  This forum is not a free consulting forum.  To understand the parameters of these technical forums please read the following and consider the limitations of the forum and the parameters of the questions asked.

    Technical Forum Limits


    \_(ツ)_/

    Wednesday, May 8, 2019 10:42 PM
  • sorry about that, I should've tried more and I did after reading your message and I got it to work!
    Wednesday, May 8, 2019 10:56 PM
  • Hi,
    Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Lee

    Just do it.

    Thursday, May 9, 2019 9:08 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Tuesday, May 14, 2019 9:48 AM