Regular expressions - length of a string
Caddy
-
hibou_4ce_rouge Posted messages 1 Status Member -
hibou_4ce_rouge Posted messages 1 Status Member -
Hello,
I have a small problem. I’m wondering if it’s possible to write a regular expression that checks the length of a string. I’d like to verify that a string contains only 4 digits.
[0-9]{4} doesn’t quite fit because it only verifies that a string contains a sequence of 4 digits and doesn’t constrain its length to 4.
Thanks for your suggestions.
Configuration: Windows XP Firefox 2.0.0.20
I have a small problem. I’m wondering if it’s possible to write a regular expression that checks the length of a string. I’d like to verify that a string contains only 4 digits.
[0-9]{4} doesn’t quite fit because it only verifies that a string contains a sequence of 4 digits and doesn’t constrain its length to 4.
Thanks for your suggestions.
Configuration: Windows XP Firefox 2.0.0.20
13 answers
-
Try to delimit your string by marking the start and end.
Example: ^[0-9]{4}$-
Hi,
Yes, but in this case it limits the length of the line and not the string.
It doesn’t work if we have a line "1234 and yet 4567 and yet 6789"
On the other hand, if the line must contain only a string of 4 digits (he didn’t specify ;-) then it’s ok.- Hi,
Yes but not the line.
A line can contain several strings of characters, right?
^ - start of line and start of string (ok)
$ - end of line before the line break character; $ can correspond to somewhere in the string if the string contains other line break characters and if the regex implementation allows it.
-
-
I think we’re getting off track. Caddy’s question seemed clear, but maybe I misunderstood it.
The discussion is starting to turn into a debate and that makes me a bit uncomfortable.
We’ll let Caddy come back and say what he really wants.
I find your solution great... And a regex is usually applied to a string, not to a line... -
Hello,
I don’t know if I’ll have the solution, but what software is this about?
--
Don’t hesitate to put your topic as solved, if that’s the case! ^^ -
Hmm...
I don’t quite understand the answer.
I’d just like to have the regular expression (if possible) that checks the length of a string (length 4 and containing 4 digits), whatever the language. -
Hello, For example
~$ echo 1234 5678 abc1234 abc123456 123456789| perl -ne 'while(/\b([0-9]{4})\b/g){ print $1, " "}print "\n"' 1234 5678 $ echo 1234 5678 abc1234 abc123456 123456789| tr ' ' '\n' 1234 5678 abc1234 abc123456 123456789 $ echo 1234 5678 abc1234 abc123456 123456789| tr ' ' '\n' |grep -Eo '\b[0-9]{4}\b' 1234 5678You must escape the word-boundary regex with word-boundary characters to obtain exactly 4 characters (digits in your case). -
In fact, simply the expression of what you asked depends on the software you are using!
Excel, C++, Java, SQL ...
--
Don't hesitate to mark your topic as resolved, if that's the case! ^^ -
I wonder if it is possible to write a regular expression that verifies the length of a string...
Not a line, but just a string...
If he wants to adapt this to a line containing several strings, he will only have to perform a split on the line by the character that will serve as the string separator, and then test the REGEX with each element of his array. -
Hi,
he will just have to split the line by the character that will serve as the string separator, and then
Splitting when you can directly apply the regex is a bit vague.
We don’t know the separator, which isn’t necessarily a space; there may be tabs and punctuation marks.
Using the start/end of word characters so that it’s either a single string per line or multiple strings, that will work.
Even the regex between ^ and $ will only accept one string (pattern) per line. -
Hi,
Don’t say that “any old thing 1243 and even 4567” is not a string ;-)
And a regex is generally applied to a string, not to a line...
A regex applies to a string, I didn’t say otherwise. To go further, regex allows you to find one or more matches in a string.
A line is a string of characters that ends with a newline character. Let’s say I may have expressed myself poorly, I agree with you on that. In fact your regex applies to a line since you look for the end of line with $. So you require the match to be a line.
Am I clearer now? ;-) -
Hi there!
So, I wanted to verify that a string not only
contains only digits but also has a length of exactly 4.
So, I tried the solution proposed by Michael-Ange^[0-9]{4}$which seems to work perfectly.
Thanks for your responses.-
Hello, Of course it works. But the concept of a string is linear; you must assimilate them. When we do ^motif$ it means that not only does the string have length 4 and contain digits, but there is only one match per line, which you did not specify, that's why I thought of a more generic solution that does not doubt Michael-Ange's relevant solution. You should also get into the habit of specifying which application you want to implement it with, since regexes differ depending on the implementation (software, language). I could have suggested, for example, ^\d{4}$, but that is not recognized by everyone. The day you look for several matches in a string, then think about word boundaries. because ^motif$ will not give you the wanted result. In this instance Michael_Ange was psychic ;-)
-
-
Glad you found your answer, but as lami20j said, next time you’ll need to be a bit more precise (language).
I think that’s what Yofa was trying to make you understand. -
-
Hello,
The topic is very old but I found a solution so I’m posting it.
I suppose you were using Notepad++ and making your expressions with search/replace.
I didn’t find a way to verify the length of a string, but you can add a check for the characters before and after your 4 digits.
That is to say, suppose you have columns separated by tabs with sequences of digits of different lengths and you only want a sequence of 4 digits.
You will include in your search a tab before and after your 4 digits, like this:
\t[0-9]{4}\t
Notepad will thus search for occurrences of this kind "tab1234tab".
It can work with spaces or other separating characters.
There you go,
I hope this helps some people.