I am completely new to regular expressions in java,
I am reading a file line by line and trying to use string.matches
for 3 characters and then 10 numbers at the beginning of the line.
In textpad I can do something like:
^[A-Z][A-Z][A-Z][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
How do I convert this in to java regex?
You have to use quantifiers. So,
x{n}
means match n
number of x exactly
x{n,}
matches x n
to many times
x{n,m}
matches x n
to m
times..
So,your regex would be
^[a-zA-Z]{3}\\d{10}
it's same, but optimized:
"^[A-Z]{3}[0-9]{10}"
or equal one
"^[A-Z]{3}\\d{10}"
You can use the method string.startsWith(). Then the first '^' is not needed.
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.