簡體   English   中英

正則表達式字母后跟數字或數字和字母

[英]Regular Expression Letter followed by numbers or numbers and a letter

我對以下正則表達式有一些困難:

G后跟1-5個數字

或G后跟4個數字,后跟單個字母AZ

有人可以幫忙嗎?

例如,有效的條目將是:

G2
G12
G123
G1234
G12345
G1234A

謝謝

^[G][0-9]{1,5}?$|^[G][0-9]{4}[A-Z]?$

^[G]表示以G開頭[0-9]{1,5}表示下一個1到5個字母是數字[0-9]{4}表示接下來的4個字母是數字[AZ]表示最后一個字母必須是字母A -Z之間。

檢測結果

試試這個: -

  G\d{4}[A-Z]|G\d{1,5}

試試這個正則表達式

^\b[G][0-9]{1,5}?$|^[G][0-9]{4}[A-Z]?$

REGEX DEMO

OP:

在此輸入圖像描述

正則表達式解釋

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  [G]                      any character of: 'G'
--------------------------------------------------------------------------------
  [0-9]{1,5}?              any character of: '0' to '9' (between 1
                           and 5 times (matching the least amount
                           possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
--------------------------------------------------------------------------------
 |                        OR
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  [G]                      any character of: 'G'
--------------------------------------------------------------------------------
  [0-9]{4}                 any character of: '0' to '9' (4 times)
--------------------------------------------------------------------------------
  [A-Z]?                   any character of: 'A' to 'Z' (optional
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM