4

How do I test to see the length of a string using regex?

For example, how do i match a string if it contains only 1 character?

2
  • 7
    What language? Why can't you use a proper function like length() or strlen()? Commented May 20, 2011 at 22:00
  • /^.$/, but I can't believe whatever language you're using doesn't have a better way of doing this. Commented May 20, 2011 at 22:00

4 Answers 4

4
^.$

But most frameworks include methods that will return string length, which you should use instead of regex for this.

Sign up to request clarification or add additional context in comments.

4 Comments

The {1} is really extraneous and just adds line-noise.
$ also can match newline, so the re would match "x" and "x\n"
@Seth In what regex flavor does $ match after a newline? The OP didn't mention an application. In JavaScript, for example, /foo$/.test("foo\n") returns false.
@Phrogz: No, the point is it matches before a newline and end of line. As for what regex flavors, System V, POSIX, Perl, Ruby, PHP, Python are the ones I know of. I'm not actually aware of any that do not work that way. Well, I wasn't until I saw your javascript example.
4

Anchor to the start and end of string and match one character. In many languages:

^.{1}$

In Ruby's Regex:

\A.{1}\z

2 Comments

The {1} doesn't add anything. The latter is correct while the former will match "x" and "x\n". You also need to ensure that . can match \n, which it often will not without the /s modifier (/m in Ruby)
@Seth The {1} doesn't add anything in the "for example" case mentioned by the OP, but the OP asked for how to test the length in general. I've added the {1} (as have others) to make it clear how to match a different number of characters. Further, whether or not $ matches after a "\n" depends on the regex flavor. As the OP has not yet specified the flavor, your statement is not absolutely correct.
1

Matching a single character would be (using Perl regex):

/\A.\z/s

\A means "start of the string", . means "any character", and \z means "end of the string". Without the \A and \z you'll match any string that's longer than one character.

Edit: But really you should be doing something like:

if( length($string) == 1 ) {
  ...
}

(using Perl as an example)

Edit2: Previously I had /^.$/ but, as Seth pointed out, this allows matches on strings that are two characters long where the last character is \n. The \A...\z construct fixes that.

2 Comments

The listed regex will match "x" and "x\n"
Except . typically means "any character but \n" unless you use the /s modifier (/m in Ruby), so "\n" would not match.
0
/^.\z/s

The above requires perl compatible regexps. The trick is that /^.$/ might match "x" and "x\n". Adding /s modifier doesn't help there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.