logistef
logistef

Reputation: 796

What are ^.* and .*$ in regular expressions?

Can someone explain the meaning of these characters. I've looked them up but I don't seem to get it.

The whole regular expression is:

/^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$/

So basicly the start of the regex and the end characters.

Upvotes: 47

Views: 219697

Answers (8)

ViAi
ViAi

Reputation: 308

^ is used in the beginning of the regEx to not consider the elements following this symbol

Eg:

String s= Test1ng-345Gt=code-Q!

regEx Pattern -> "[^A-Za-z0-9]" Will give output-> "-=-!"

Check this site its helpful->https://regex101.com/

Upvotes: -2

Till
Till

Reputation: 3154

^ matches the beginning of a string

$ matches the end

.* is any number of characters

Upvotes: 5

shredder
shredder

Reputation: 401

^.* //Start of string followed by zero or more of any character (except line break)

.*$ //Zero or more of any character (except line break) followed by end of string

So when you see this...

(?=.*[@#$%^&+=]).*$

It allows any character (except line break) to come between (?=.*[@#$%^&+=]) and the end of the string.

To show that . doesn't match any character, try this:

/./.test('\n');  is false

To actually match any character you need something more like [\s\S].

/[\s\S]/.test('\n') is true

Upvotes: 28

AsTeR
AsTeR

Reputation: 7521

This matches the beginning of the line (^) followed by any character (.*) :

^.*

This matches the end of the line ($) preceded by any character (.*) :

.*$

Upvotes: 5

Till Helge
Till Helge

Reputation: 9311

  • . means "any character".
  • * means "any number of this".
  • .* therefore means an arbitrary string of arbitrary length.
  • ^ indicates the beginning of the string.
  • $ indicates the end of the string.

The regular expression says: There may be any number of characters between the expression (?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]) and the beginning and end of the string that is searched.

Upvotes: 92

Alan Moore
Alan Moore

Reputation: 75222

That looks like a typical password validation regex, except it has couple of errors. First, the .* at the beginning doesn't belong there. If any of those lookaheads doesn't succeed at the beginning of the string, there's no point applying them again at the next position, or the next, etc..

Second, while the regex insures that each of those three kinds of character is present, it doesn't say anything about the rest of the string. That may have been a deliberate choice, but people usually try to insure that only those kinds of characters are present. In that case, you would want to change the first lookahead from (?=.{8,}) to (?=[A-Za-z@#$%^&+=]{8,}$).

End result:

/^(?=[A-Za-z@#$%^&+=]{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$/

Upvotes: 0

FailedDev
FailedDev

Reputation: 26930

Something else that my help you in the future:

.*$

will match two times given this string : "1"

If you are wondering why, it's because it consumes all characters, but then also matches nothing. So the empty string is also a match.

Upvotes: 0

Marc B
Marc B

Reputation: 360602

Main docs: http://www.php.net/manual/en/reference.pcre.pattern.syntax.php

/^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$/
12345   6         7                                  89

1 - start of pattern, can be almost any character, and must have a matching character at the END of the pattern (see #9 below)
2 - anchors the pattern to the start of a line of text
3 - `.` matches any character
4 - a modifier, "0 or more of whatever came before"
  - `.*` means "0 or more of ANY characters"
5 - A positive lookahead assertion: http://www.php.net/manual/en/regexp.reference.assertions.php
6 - A repetition indictor: http://www.php.net/manual/en/regexp.reference.repetition.php
  - `{8,}` = "at least 8 of whatever came previously"
  - `.{8,}` = "at least 8 'any' characters"
7 - A character class: http://www.php.net/manual/en/regexp.reference.character-classes.php
  - `[a-z]` - any one character in the range 'a' - 'z' (the lower case alphabet)
8 - anchors the pattern to the end of the line
9 - end of the pattern, must match character used in #1 above.

Upvotes: 13

Related Questions