subst ';' STRING ';' REGEX (';' STRING (';' NUMBER )? )?
Macro
Summary
Perform regex-based substring substitution on a target string, with optional replacement limit.
Syntax
${subst;<target>;<regex>[;<replacement>[;<count>]]}
Parameters
- target: String in which to perform substitutions
- regex: Regular expression pattern to match
- replacement: (Optional) Replacement string for matches. Defaults to empty string.
- count: (Optional) Maximum number of replacements to perform. Defaults to all matches.
Behavior
The macro:
- Finds all substrings in
targetthat matchregex - Replaces them with
replacement(or empty string if not specified) - Limits replacements to
countif specified (otherwise replaces all) - Returns the modified string
This is similar to Java’s String.replaceAll() but with optional replacement count.
Examples
# Remove file extension
${subst;foo.bar;\.bar}
# Returns: foo
# Replace dots with dashes
${subst;com.example.package;\.;-}
# Returns: com-example-package
# Replace with specified text
${subst;Hello World;World;Universe}
# Returns: Hello Universe
# Limit replacements
${subst;a,b,c,d;,;-;2}
# Returns: a-b-c,d (only first 2 replacements)
# Remove all digits
${subst;version1.2.3;\d}
# Returns: version.. (empty replacement)
# Replace spaces with underscores
${subst;My Project Name; ;_}
# Returns: My_Project_Name
# Complex regex - remove version info
${subst;bundle-1.0.0.jar;-[\d.]+\.jar;.jar}
# Returns: bundle.jar
# Multiple occurrences
${subst;test-test-test;test;prod}
# Returns: prod-prod-prod
# With capture groups
${subst;com.example.api;com\.(.*)\.api;org.$1.impl}
# Returns: org.example.impl
Use Cases
- Path Manipulation: Modify file paths or package names
- String Cleaning: Remove unwanted patterns from strings
- Format Conversion: Transform string formats
- Version Stripping: Remove version numbers from names
- Pattern Replacement: Replace specific patterns with alternatives
Notes
- Uses Java regular expression syntax
- Default replacement is empty string (effectively removes matches)
- Default count is unlimited (replaces all matches)
- Replacement string can contain capture group references (
$1,$2, etc.) - To use literal
$in replacement, escape it as$$ - The count parameter limits the number of replacements, not matches
- For more complex transformations, consider replacestring
Related Macros
- replace / replacestring - Replace in strings with patterns
- replacelist - Replace patterns in list elements
- filter - Filter list elements by regex
- substring - Extract substring by position
See test cases in MacroTestsForDocsExamples.java
