Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Explanation ^.*$ [duplicate]

Tags:

regex

When I use this code:

'DTH' + @fileDate + '^.*$'  

I get DTH201510080900.xlsx

What does ^.*$ do? Does that give me the 0900 time?

like image 689
Maria Torres Avatar asked Oct 08 '15 17:10

Maria Torres


People also ask

How do you match duplicate words in regex?

Following example shows how to search duplicate words in a regular expression by using p. matcher() method and m. group() method of regex. Matcher class.

What does *$ mean in regex?

*$ means - match, from beginning to end, any character that appears zero or more times. Basically, that means - match everything from start to end of the string. This regex pattern is not very useful. Let's take a regex pattern that may be a bit useful.

Why are there two backslashes in regex?

A single backslash means escape, so a second one after it gets escaped, meaning the double backslash matches a single backslash. – Paul S.


1 Answers

  • ^ matches position just before the first character of the string
  • $ matches position just after the last character of the string
  • . matches a single character. Does not matter what character it is, except newline
  • * matches preceding match zero or more times

So, ^.*$ means - match, from beginning to end, any character that appears zero or more times. Basically, that means - match everything from start to end of the string. This regex pattern is not very useful.

Let's take a regex pattern that may be a bit useful. Let's say I have two strings The bat of Matt Jones and Matthew's last name is Jones. The pattern ^Matt.*Jones$ will match Matthew's last name is Jones. Why? The pattern says - the string should start with Matt and end with Jones and there can be zero or more characters (any characters) in between them.

Feel free to use an online tool like https://regex101.com/ to test out regex patterns and strings.

like image 174
zedfoxus Avatar answered Sep 20 '22 13:09

zedfoxus