Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex exact length of whole string

Tags:

regex

I want to match a string of exact 3 length. I am using the following regex

("\\d?[A-Za-z]{2,3}\d?")

Here the string can have 1 digit either at start or at end of the string, or the string can have 3 letters.Is there any way to define length of the matching string like :

("(\\d?[A-Za-z]{2,3}\d?){3}") // it does not work

I have another solution of it.

("(\\d[A-Za-z]{2})|([A-Za-z]{2}\\d)|([A-Za-z]{3})")

But I just want to know if there is any way to define length of whole matching string.

like image 352
Dinesh Choudhary Avatar asked Feb 09 '23 05:02

Dinesh Choudhary


1 Answers

^.{3}$

If this isn't really your answer you need to specify it better. You have zero solutions not several. What exactly are you trying to match. Give a couple examples.

http://www.regexplanet.com/advanced/java/index.html

^(\d[a-zA-Z]{2}|[a-zA-Z]{2}\d|[a-zA-Z]{3})$  

If you want that letters and numbers thing.

If you want the extra stuff at the end to be possible without the string being over you can just look for the space afterwards.

^(\d[a-zA-Z]{2}|[a-zA-Z]{2}\d|[a-zA-Z]{3})\s

From the comments:

So it's

^[^\s]{3}\s\d{7}\s.\d{6}

? -- '^' start of line, '[^\s]' not a space. '{3}' three of those. '\s' a space. '\d' a digit. '{7}' seven of those. '\s' a space. '.' some character. '\d' a digit. '{6}' of those.

Regex is basically just programmatically a way of describing what you're looking for. If you can properly form the question of what you want to match it's easy to write that directly in regex.

like image 91
Tatarize Avatar answered Apr 09 '23 19:04

Tatarize