0

how to check the string if the last 7 chars contains numbers or not using regex in java.

Vamsi
  • 25
  • 1
  • 5
  • [0-9] <- Match any of the digits 0-9 {7} <- Has to be 7 digits in a row $ <- These 7 digits have to be right before a newline – jh316 Dec 25 '21 at 06:55
  • can you give any example string which matches that pattern.. I didn't understood what right before newline means.. – Vamsi Dec 25 '21 at 07:12
  • sfasdofjao1234567 matches 1234567. aspfkaspf0923523 matches 0923523. 1234567abcde0987654 only matches 0987654, because it ends right before a newline (end of line), while 1234567 doesn't. – jh316 Dec 25 '21 at 07:21

1 Answers1

0
[0-9] = will match any digit,
{7} = the length of the digits, so the matched string will be exactly 7 digits long
$ = signifies end of line

so this regex will match any line that ends with 7 digits

a good way to start learning is sites like https://regex101.com/, which explain the whole regex string you input

Ron
  • 5,900
  • 2
  • 20
  • 30