[Python][en] Regex – Match Start or End of String

Python 8 Th10 2022

1. Line Anchors

In regex, the anchors have zero width. They are not used for matching characters. Rather they match a position i.e. before, after, or between characters.

To match the start or the end of a line, we use the following anchors:

Caret (^) matches the position before the first character in the string.
Dollar ($) matches the position right after the last character in the string.

RegexStringMatches
^aabcMatches a
c$abcMatches c
^[a-zA-Z]+$abcMatches abc
^[abc]$abcMatches a or b or c
[^abc]abcDoes not match. A matching string begins with any character but a,b,c.
^[mts][aeiou]motherMatches. Searches for words that start with m, t or s. Then immediately followed by a vowel.
[^n]g$king
ng
Does not match. The string should end with g, but not ng.
[^k]g$kongMatches.
^g.+g$gangMatches. Word would start and end with g. Any number of letters in between.

2. Regex to Match Start of Line

"^<insertPatternHere>"

  • The caret ^ matches the position before the first character in the string.
  • Applying ^h to howtodoinjava matches h.
  • Applying ^t to howtodoinjava does not match anything because it expects the string to start with t.
  • If we have a multi-line string, by default caret symbol matches the position before the very first character in the whole string. To match the position before the first character of any line, we must enable the multi-line mode in the regular expression.
  • In this case, caret changes from matching at only the start the entire string to the start of any line within the string.
DescriptionMatching Pattern
The line starts with a number“^\\d” or “^[0-9]”
The line starts with a character“^[a-z]” or “^[A-Z]”
The line starts with a character (case-insensitive)“^[a-zA-Z]”
The line starts with a word“^word”
The line starts with a special character“^[!@#\\$%\\^\\&*\\)\\(+=._-]”

3. Regex to Match End of Line

"<insertPatternHere>$"

  • The dollar $ matches the position after the last character in the string.
  • Applying a$ to howtodoinjava matches a.
  • Applying v$ to howtodoinjava does not match anything because it expects the string to end with v.
  • If we have a multi-line string, by default dollar symbol matches the position after the very last character in the whole string.
  • To match the position after the last character of any line, we must enable the multi-line mode in the regular expression. In this case, dollar changes from matching at only the last the entire string to the last of any line within the string.
DescriptionMatching Pattern
The line ends with a number“\\d$” or “[0-9]$”
The line ends with a character“[a-z]$” or “[A-Z]$”
The line ends with a character (case-insensitive)[a-zA-Z]$
The line ends with a word“word$”
The line ends with a special character“[!@#\\$%\\^\\&*\\)\\(+=._-]$”

Tags

Tony Phạm

Là một người thích vọc vạch và tò mò với tất cả các lĩnh vực từ khoa học tự nhiên, lập trình, thiết kế đến ... triết học. Luôn mong muốn chia sẻ những điều thú vị mà bản thân khám phá được.