简体   繁体   中英

Regular Expression to Match character at a position and ignore characters after

Want to match the character at position 7 to either be - or an Uppercase letter

This is what I have ^.{6}[-(AZ)]

Though this matches the first 7 characters, it doesn't match the whole string. Any help appreciated.

I am using Java and wanting .matches() to return true for this String

Though this matches the first 7 characters, it doesn't match the whole string.

That's the right explanation of what is going on. You can skip over the rest of the string by adding .* at the end. Additionally, the ^ anchor at the front of the expression is implied, so you can drop it for a pattern of

.{6}[A-Z-].*

As mentioned You can use .* to match anything after your specific character so use

^.{6}[-A-Z].*

and also no need of () if you don't want to capture that specific character

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM