Record Class PatternMatch

java.lang.Object
java.lang.Record
cloud.opencode.base.string.match.PatternMatch
Record Components:
pattern - the pattern that was matched | 匹配的模式
start - the start index in the text (inclusive) | 在文本中的起始索引(包含)
end - the end index in the text (exclusive) | 在文本中的结束索引(不包含)

Security | 安全性:

  • Thread-safe: Yes (record is immutable) - 线程安全: 是(记录不可变)
matchedText - the actual text that was matched | 实际匹配的文本

public record PatternMatch(String pattern, int start, int end, String matchedText) extends Record
Pattern Match Result - Represents a pattern match found in text 模式匹配结果 - 表示在文本中找到的模式匹配

Contains the matched pattern, its position, and the actual matched text.

包含匹配的模式、位置和实际匹配的文本。

Features | 主要功能:

  • Match position and length tracking - 匹配位置和长度跟踪
  • Overlap detection between matches - 匹配重叠检测
  • Text extraction from original source - 从原始源提取文本

Usage Examples | 使用示例:

PatternMatch match = ...;
String pattern = match.pattern();         // The pattern that matched
int start = match.start();                // Start index (inclusive)
int end = match.end();                    // End index (exclusive)
String text = match.matchedText();        // The actual matched text
int length = match.length();              // Length of the match
Since:
JDK 25, opencode-base-string V1.2.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • PatternMatch

      public PatternMatch(String pattern, int start, int end, String matchedText)
      Creates an instance of a PatternMatch record class.
      Parameters:
      pattern - the value for the pattern record component
      start - the value for the start record component
      end - the value for the end record component
      matchedText - the value for the matchedText record component
  • Method Details

    • length

      public int length()
      Returns the length of the match. 返回匹配的长度。
      Returns:
      match length | 匹配长度
    • overlaps

      public boolean overlaps(PatternMatch other)
      Checks if this match overlaps with another. 检查此匹配是否与另一个重叠。
      Parameters:
      other - the other match | 另一个匹配
      Returns:
      true if overlapping | 如果重叠则返回true
    • contains

      public boolean contains(PatternMatch other)
      Checks if this match contains another. 检查此匹配是否包含另一个。
      Parameters:
      other - the other match | 另一个匹配
      Returns:
      true if this contains other | 如果此匹配包含另一个则返回true
    • extractFrom

      public String extractFrom(String originalText)
      Extracts the matched portion from the original text. 从原始文本中提取匹配部分。
      Parameters:
      originalText - the original text | 原始文本
      Returns:
      the matched portion | 匹配部分
    • toDisplayString

      public String toDisplayString()
      Returns a formatted string representation. 返回格式化的字符串表示。
      Returns:
      formatted string | 格式化字符串
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • pattern

      public String pattern()
      Returns the value of the pattern record component.
      Returns:
      the value of the pattern record component
    • start

      public int start()
      Returns the value of the start record component.
      Returns:
      the value of the start record component
    • end

      public int end()
      Returns the value of the end record component.
      Returns:
      the value of the end record component
    • matchedText

      public String matchedText()
      Returns the value of the matchedText record component.
      Returns:
      the value of the matchedText record component