Tuesday, 12 April 2016

Regex Explanation ^.*$





When I use this code:



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



I get DTH201510080900.xlsx



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


Answer




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


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...