Lucien
Lucien

Reputation: 896

Regex for Lines Under 50 Characters

I have tried (^[.*]{1,50}$)/gm but it simply does not work.

I'd like a line made up of any characters to match this regex.

Qwertyuiop
$$%%^^89e7hbequdwanjk
etc should all match, including this line

However, lines over 50 characters long should not match.

Upvotes: 1

Views: 259

Answers (2)

tripleee
tripleee

Reputation: 189739

You are specifying a string of 1-50 occurrences of either . or *. If you want a string of any characters, the [...] character class is wrong (it enumerates literal characters you want to match); you are looking for . without square brackets, which matches any one character.

The regular expression for that is

^.{1,50}$

Some languages require you to specify a separator such as /.../ around your regex, but it's hard to tell from your example whether yours is one of them; in this case, you are missing the beginning separator.

The /g flag only makes sense if you need to find multiple occurrences on the same line. The /m flag makes sense if the ^ and $ anchors should match newlines in multi-line text.

If the title of your question is correct, and you want properly under 50 characters, change the 50 to 49 (and maybe the 1 to 0).

Upvotes: 4

SamWhan
SamWhan

Reputation: 8332

Your regex, [.*] matches only dots . and *, since inside [] both are treated literally. Try

/^.{1,50}$/gm

It'll match between 1 and 50 of anything. If you also want to capture it add back the parenthesis

/(^.{1,50}$)/gm

Upvotes: 1

Related Questions