Technical Analysis of Regular Expression Exact End-of-String Matching

Nov 23, 2025 · Programming · 8 views · 7.8

Keywords: Regular Expression | End Anchor | String Matching | File Extension | Pattern Matching

Abstract: This paper provides an in-depth exploration of anchor character usage in regular expressions, focusing on the mechanism of the $ symbol in matching string endings. Through practical file extension matching cases, it analyzes how to avoid false matches and offers complete regex solutions with code examples. The article also discusses matching behavior differences in multi-line mode and application considerations in real programming scenarios.

Fundamental Principles of Regex End Anchors

In regular expression syntax, anchor characters are used to specify special requirements for match positions. The $ symbol serves as an end anchor, requiring that matches must occur at the end of the string. This feature plays a crucial role in exact matching scenarios, particularly when distinguishing target strings from others containing identical substrings.

Practical Problem in File Extension Matching

Consider a typical file processing scenario: the need to filter files with specific extensions while dealing with filenames containing identical substrings. For example, in a file system, two files exist: B82177_2014-07-08T141507758Z.ccf and B82177_2014-07-08T141507758Z.ccf.done. Using the simple pattern .*\.ccf would match both files because this pattern only requires the string to contain the .ccf substring, regardless of its position.

Solution: Using End Anchors

The correct solution involves adding the $ anchor character at the end of the pattern, forming .*\.ccf$. This pattern specifically means:

Through this combination, only strings ending with .ccf will be matched, thus precisely filtering the target files.

Code Implementation Examples

Below are specific implementations in different programming languages:

Python Implementation

import re

files = [
    "B82177_2014-07-08T141507758Z.ccf",
    "B82177_2014-07-08T141507758Z.ccf.done"
]

pattern = r".*\.ccf$"
for file in files:
    if re.match(pattern, file):
        print(f"Matched file: {file}")

Java Implementation

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class FileMatcher {
    public static void main(String[] args) {
        String[] files = {
            "B82177_2014-07-08T141507758Z.ccf",
            "B82177_2014-07-08T141507758Z.ccf.done"
        };
        
        Pattern pattern = Pattern.compile(".*\\.ccf$");
        for (String file : files) {
            Matcher matcher = pattern.matcher(file);
            if (matcher.matches()) {
                System.out.println("Matched file: " + file);
            }
        }
    }
}

Behavior Differences in Multi-line Mode

It is particularly important to note that in multi-line mode, the behavior of $ changes. In standard mode, $ only matches the end of the entire string; in multi-line mode, $ also matches the end of each line. This difference is especially significant when parsing multi-line text.

Considerations in Practical Applications

When using end anchors in actual development, the following factors should be considered:

Extended Application Scenarios

Beyond file extension matching, end anchor techniques can be applied to:

By appropriately utilizing the anchor features of regular expressions, the accuracy and efficiency of string matching can be significantly improved.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.