开发者

Regular expression matching specific letter combos

开发者 https://www.devze.com 2023-02-27 08:25 出处:网络
I need to match the following example strings: LA20517505 BN30116471 I tried this: [LA|BN].\\d{8} That does indeed match, but it also matches other letters as well. I spec开发者_StackOverflowifical

I need to match the following example strings:

LA20517505 BN30116471

I tried this: [LA|BN].\d{8}

That does indeed match, but it also matches other letters as well. I spec开发者_StackOverflowifically need to match "LA" or "BN" followed by 8 numbers.


Don't use brackets here but parenthesis : (LA|BN)\d{8}

Explanation:

(LA|BN) Match character sequences LA or BN
\d{8}   followed by 8 digits

whereas the initial regex [LA|BN].\d{8} can be read as :

[LA|BN]  Match either character L,A,|,B or N 
.        Match any character
\d{8}   followed by 8 digits
0

精彩评论

暂无评论...
验证码 换一张
取 消