Find lines of length 10 with regex
What are all these answers with "7.{9}"
? They will match things like "7abc efghi" which is obviously two lines (in my opinion, based on the tone of the question). They will also match "7999999999999999999999999999" (which is clearly wrong based on the question) unless you make it clear that you are using an RE function with implicit line start and end boundaries.
What you probably want is a whitelist of possible characters typing a string like:
7[A-Za-z0-9]{9}
with (potentially) word boundaries on either side, or (for older RE engines without this feature), all of them:
[^A-Za-z0-9]7[A-Za-z0-9]{9}[^A-Za-z0-9]
^7[A-Za-z0-9]{9}[^A-Za-z0-9]
[^A-Za-z0-9]7[A-Za-z0-9]{9}$
^7[A-Za-z0-9]{9}$
If there are more characters that you think are part of the "line", just add them to the sections "[A-Za-z0-9]"
.
source to share
This template should work: 7.{9}
" 7
" will obviously match the number " 7
". " .
" will match any character, and " {9}
" will match the previous character or group (in this case, " .
") 9 times.
It's good to spend some time learning about regular expressions - you can take a look at this article or this cheat sheet to help you understand how they work. A tool like RegexBuddy or Regular Expression Builder (my personal favorite is simple and powerful) can be a huge help too.
source to share
To find strings (not words) 10 characters long:
^7\S{9}$
^ - begin of the string 7 - string must start with 7 \ S {9} - 9 characters (use. {9} if you wish to allow any symbol in string including whitespaces) $ - end of the string
If this is homework, I would recommend using the Regex Builder to test your knowledge of regular expressions.
source to share
Like what Pax said, if you want to match a string that doesn't match a space, that is:
// match this:
715kdgbp94
// not this
7 539 136a
then you can use this regex:
7\S{9}
It's also worth noting that it's not very convenient to use regular expressions, and that some fairly standard and much more straightforward methods probably exist in whatever language you use. For example, in PHP:
if (strlen($myString) == 10 && $myString[0] == "7")) {
or Javascript:
if (myString.length == 10 && substr(myString, 0, 1) == "7") {
source to share
\b7\w{9}\b
As in "a continuous sequence of word symbols starting with '7'
and standing by itself."
(?<=^7|\s)7\w{9}(?=$|\s)
As in "a contiguous sequence of word characters that begins with '7'
and is enclosed in white space."
The latter implies that support for look-behind and look-ahead is supported, obviously.
source to share