I want to use a regular expression to find strings which are exactly 10 characters long and begin with "7". Please could someone tell me how?
This ought to work:
^7.{9}$
The 7 matches the character '7'. The . matches any character. The {9} tells it to match the previous symbol 9 times.
Edit: The ^ matches the beginning of the string and the $ matches the end of the string.
This pattern should work: 7.{9}
The '7
' will obviously match the digit '7
'. The '.
' will match any character, and the '{9}
' will match the preceding character or group (in this case, '.
') 9 times.
It's helpful to spend some time learning about Regular Expressions -- you might take a look at this article or this "cheat sheet" to help you get a feel for how they work. A tool like RegexBuddy or Regular Expression Builder (my personal favorite -- simple and powerful) can also be a huge help.
7.{9}
Yeah, probably not what you're looking for, but with what information you've provided, it's exactly what you asked for.
"beginning with 7" - do you mean beginning with the number 7?
Anyway, to match a string that's exactly 10 characters long:
/[a-z0-9]{10}/i
That will match any string made up of letters and numbers. It's easy enough to add additional characters to match, but since you didn't specify...
Good luck
What are all these answers with "7.{9}"
? They will match things like "7abc efghi" which is clearly two strings (in my opinion, based on the tone of the question). They'll also match "7999999999999999999999999999" (which is clearly wrong based on the question) unless you make it clear you're using an RE function with implicit start/end line boundaries.
What you probably need is a whitelist of possibile characters amking up a string, something like:
7[A-Za-z0-9]{9}
with (potentially) word boundaries on either side, or (for older RE engines without that feature), all of these ones:
[^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 would consider part of a "string", simply add them to the "[A-Za-z0-9]"
sections.
this will match any number that starts with 7 that is no longer than 10 digits.
\b7\d{0,9}\b
Something like this maybe:
(^|\n)(7\S{9})\s.*
Where (^|\n) is the start of the string (either bof, or new line), (7\S{9}) is the string we want, \S is any non-whitespace character, \s is any whitespace, and .* means anything else we don't want.
Like what Pax said, if you want to match a string which doesn't match any whitespace, that is:
// match this:
715kdgbp94
// not this
7 539 136a
then you could use this regex:
7\S{9}
It's also worth pointing out that this isn't a great use of regexes, and that some fairly standard and much more understandable methods would probably exist in whatever language you are using. For example, in PHP:
if (strlen($myString) == 10 && $myString[0] == "7")) {
or Javascript:
if (myString.length == 10 && substr(myString, 0, 1) == "7") {
To find strings (not words) that are exactly 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 it is a homework I'd recommend to use Regex Builder to test your knowledge of regular expressions.
As far as I'm concerned embedded white space is not an issue, use anchoring to make sure the string is not too long
^7.{9}$
\b7\w{9}\b
As in "continuous series of word characters that starts with '7'
and stands on its own".
(?<=^7|\s)7\w{9}(?=$|\s)
As in "continuous series of word characters that that starts with '7'
and is enclosed in white space".
The latter implies that look-behind and look-ahead are supported, obviously.