locked
Need RegEx pattern - very rusty RRS feed

  • Question

  • User-718146471 posted

    Good morning all. It has been a while since I last played with RegEx and would appreciate some help developing a pattern. What I'm looking for is in a list, there are certain items that have a similar pattern. I would like to fish these out using RegEx.

    The text pattern looks like this:

    DF\123456

    I came up with one that is close but I want to make the non-word character \ be specifically that.

    [a-zA-Z{2}]\S[0-9{6}]

    Again, my regex is rusty so I could use some help with this. Thanks!

    Friday, January 5, 2018 2:25 PM

Answers

  • User2103319870 posted

    bbcompent1

    I want to make the non-word character \ be specifically that

    You can escape slash with another slash like below

    ^[a-zA-Z]{2}\\[0-9]{6}$

    Explanation

    [a-zA-Z]       # Match a single character present in the list below
                   # A character in the range between "a" and "z"
                   # A character in the range between "A" and "Z"
       {2}         # Exactly 2 times
    \\             # matches the character \ literally 
    [0-9]          # Match a single character in the range between "0" and "9"
       {6}         # Exactly 6 times
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 5, 2018 3:10 PM

All replies

  • User2103319870 posted

    bbcompent1

    I want to make the non-word character \ be specifically that

    You can escape slash with another slash like below

    ^[a-zA-Z]{2}\\[0-9]{6}$

    Explanation

    [a-zA-Z]       # Match a single character present in the list below
                   # A character in the range between "a" and "z"
                   # A character in the range between "A" and "Z"
       {2}         # Exactly 2 times
    \\             # matches the character \ literally 
    [0-9]          # Match a single character in the range between "0" and "9"
       {6}         # Exactly 6 times
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 5, 2018 3:10 PM
  • User-718146471 posted

    Thank you A2H, very clear, concise and easy to understand :)

    Friday, January 5, 2018 3:18 PM
  • User303363814 posted

    Why a Regex?

    inStr.Split('\\');

    My 2c - this is waaaay clearer and easier to maintain than a regex

    Friday, January 5, 2018 8:47 PM