Regex code for any string containing BOTH letters AND digits and possibly underscores and dashes

后端 未结 1 791
梦如初夏
梦如初夏 2020-12-19 19:46

My regex knowledge is pretty limited, but I\'m trying to write/find an expression that will capture the following string types in a document:

DO match:

相关标签:
1条回答
  • 2020-12-19 19:56

    You can use the following expression with the case-insensitive mode:

    \b((?:[a-z]+\S*\d+|\d\S*[a-z]+)[a-z\d_-]*)\b
    

    Explanation:

    \b                   # Assert position at a word boundary
    (                    # Beginning of capturing group 1
      (?:                # Beginning of the non-capturing group
        [a-z]+\S*\d+     # Match letters followed by numbers
        |                # OR
        \d+\S*[a-z]+     # Match numbers followed by letters
      )                  # End of the group
      [a-z\d_-]*         # Match letter, digit, '_', or '-' 0 or more times
    )                    # End of capturing group 1
    \b                   # Assert position at a word boundary
    

    Regex101 Demo

    0 讨论(0)
提交回复
热议问题