1

I recently read about these 2 punctuation, and found it really confusing.

A pattern is a sequence of pattern items. A caret '^' at the beginning of a pattern anchors the match at the beginning of the subject string. A '$' at the end of a pattern anchors the match at the end of the subject string. At other positions, '^' and '$' have no special meaning and represent themselves.(Reference Manual 5.3)

Anchor? what does that mean in this explanation? The match at the end/beginning of the subject string? The Beginning/End of the subject string, what's that?

Aki
  • 2,818
  • 1
  • 15
  • 23
BlaztOne
  • 180
  • 1
  • 10
  • 1
    Not familiar with `lua` but what you have quoted sounds like regex definition. Are you asking what an `Anchor` is? – user3783243 Oct 26 '20 at 23:37
  • I haven't learnt about regex, but yes, lua string library has many similarity with regex – BlaztOne Oct 26 '20 at 23:38
  • So I think *anchor* here is a definition in regex, isn't it? – BlaztOne Oct 26 '20 at 23:39
  • 2
    `^A` would mean a string must have its first character as an `A`. `Z$` would mean the last character of a string would have to be a `Z`. An example of real world usage could be `^192[.]168[.]` which would make sure an IP started with `192.168.` The last sentence sounds incorrect. A `$` with text after it would never match unless the`$` were escaped. – user3783243 Oct 26 '20 at 23:39
  • Nice answer, but why do you need to use **[.]** instead of the normal **.** ? – BlaztOne Oct 26 '20 at 23:54
  • 2
    He uses `[]` to escape `.`. Raw `.` means "any single character" in a pattern. – Deduplicator Oct 27 '20 at 01:20
  • 1
    A backslash or `[]` can be used. Depending on regex engine though the backslash may not be supported for escaping, or may require 2-3 backslashes. – user3783243 Oct 27 '20 at 01:35
  • 1
    Does this answer your question? [What do ^ and $ mean in a regular expression?](https://stackoverflow.com/questions/6908725/what-do-and-mean-in-a-regular-expression) – user3783243 Oct 27 '20 at 01:36
  • 1
    @user3783243, it's `%` in Lua, not backslash. – Alexander Mashin Oct 27 '20 at 02:43
  • @AlexanderMashin, Please note that Lua doesn't use regex by default, the tag you have added is not applicable here. – Aki Oct 27 '20 at 10:21
  • @BlaztOne, Core of your problem is that you assumed *anchor* is used as a noun. It's not. It's a verb as indicated by "s" ("anchor*s*"). You could use other verbs here: to tie or to bind. All in all, you end up with what 3783243 has written. – Aki Oct 27 '20 at 10:34
  • 1
    Why nobody provides an answer instead of just commenting? This seems to be a legit question, isn't it? – wp78de Oct 30 '20 at 18:11

0 Answers0