0

Im sure this has been posted before but I am having trouble locating an answer.

preg_match("/^[a-zA-Z0-9 -\.]{1,25}+$/i", ...

The regular expression above allows for all alphabetic characters, all numeric characters, and the following (,-,.). It also limits whatever string we are checking against to max 25 characters total. What I cannot understand is the purpose of +$/i. I can find most of those characters in documentation but do not understand why they are needed. The only one I cannot find any information on is i.

Edit: I suppose the $ ties into our use of the ^ character?

Edit2: Thanks to comments below it seems the i makes the expression case insensitive. Still looking for information in regards to the other characters.

Newb 4 You BB
  • 1,205
  • 1
  • 11
  • 30
  • 1
    `/i` makes the regex case insensitive. – Tim Biegeleisen Apr 14 '20 at 17:06
  • 1
    [Official reference](https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php) – msg Apr 14 '20 at 17:07
  • Does this answer your question? [How do I make this preg\_match case insensitive?](https://stackoverflow.com/questions/12411037/how-do-i-make-this-preg-match-case-insensitive) – msg Apr 14 '20 at 17:10
  • 2
    The `-` in a character class should always be first or last. Do not put it in the middle of a character class, that creates a range. The `.` in a character class does not need to be escaped. – user3783243 Apr 14 '20 at 17:14
  • 1
    Be aware that ` -.` in a character class matches all characters between space and dot, have a look at an [ASCII table](https://www.ascii-code.com/). – Toto Apr 14 '20 at 18:28

1 Answers1

1

The /i flag at the end of the regex makes the preceding pattern case insensitive. So actually, you could have just used this:

preg_match("/^[a-z0-9 -\.]{1,25}+$/i", ...

That is, in /i mode, preg_match will match a-z for both lowercase and uppercase letters, so you need only specify one range.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360