Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

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.

like image 540
logistef Avatar asked Nov 30 '11 14:11

logistef


People also ask

What does * mean in regular expression?

The asterisk ( * ): The asterisk is known as a repeater symbol, meaning the preceding character can be found 0 or more times. For example, the regular expression ca*t will match the strings ct, cat, caat, caaat, etc.

What is * and in regular expression?

. * therefore means an arbitrary string of arbitrary length. ^ indicates the beginning of the string. $ indicates the end of the string.

What does * do in regex?

The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern. `*' represents this operator. For example, `o*' matches any string made up of zero or more `o' s.

What does Star mean in regular expression?

The character * in a regular expression means "match the preceding character zero or many times". For example A* matches any number (including zero) of character 'A'. Regular Expression. Matches.


2 Answers

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

like image 175
Till Helge Avatar answered Sep 21 '22 17:09

Till Helge


^.* //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 
like image 21
shredder Avatar answered Sep 17 '22 17:09

shredder