Find lines of length 10 with regex

I want to use a regular expression to find strings that are 10 characters long and start with "7". Please can someone tell me how?

+2


source to share


11 replies


This should work:

^7.{9}$

      



7 matches the character "7" .. matches any character. {9} tells him to match the previous character 9 times.

Edit: ^ matches the beginning of the line and $ matches the end of the line.

+13


source


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]"

.

+8


source


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.

+4


source


Something like this could be:

(^|\n)(7\S{9})\s.*

      

Where (^ | \ n) is the beginning of a line (either bof or a newline), (7 \ S {9}) is the desired string, \ S is any character without spaces, \ s is any spaces, and. * means whatever we don't want.

+3


source


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.

+3


source


Inline white space is not a problem as far as I know, use anchor to make sure the string is not too long

^7.{9}$

      

+3


source


"starting at 7" - you mean starting at number 7?

Anyway, to match a string that's only 10 characters long:

/[a-z0-9]{10}/i

      

This will match any string of letters and numbers. It's easy enough to add extra characters to match, but since you didn't specify ...

Good luck.

+2


source


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") {

      

+2


source


7.{9}

Yes, probably not what you are looking for, but with what information you provided exactly what you asked for.

0


source


this will match any number that starts with 7 that does not exceed 10 digits.

\b7\d{0,9}\b

      

0


source


\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.

0


source







All Articles