cancel
Showing results for 
Search instead for 
Did you mean: 
amitdange15
Staff
Staff
Status: New

 

Build a helper function that simplifies extracting data from a string using a regular expression. This would be especially useful for parsing logs, validating formats, or extracting structured data from unstructured text.

  • Takes in a text input and a regular expression.

  • Returns the first matched group from the regex.

  • If no match is found, return null.

String getText(String input, String regex)

Ex. getText("User: John, Age: 30", "User: (\\w+)") // Output: "John"

Benefit: This function simplifies data extraction from unstructured text, accelerating development and reducing errors in input parsing and validation workflows.

 

1 Comment

Expanded Regex Functionality for Chorus Design

  1. Core Extraction Function
getText(String input, String regex, [Integer groupIndex = 1])
  • Takes a text input and a regular expression
  • Returns the matched group from the regex (defaults to first group)
  • Returns null if no match is found
  • Example: getText("Name: John Doe, ID: 12345", "Name: ([\\w\\s]+), ID: (\\d+)", 2) // Returns "12345"
  1. Advanced Extraction Options
getTextAll(String input, String regex)
  • Returns an array of all matches (not just the first)
  • Example: getTextAll("Tags: #home #garden #diy", "#(\\w+)") // Returns ["home", "garden", "diy"]
getNamedText(String input, String regex, String groupName)
  • Support for named capture groups
  • Example: getNamedText("Price: $19.99", "Price: \\$(?<amount>[\\d.]+)", "amount") // Returns "19.99"
  1. Text Manipulation Functions
regexSplit(String input, String regex)
  • Split string based on pattern matches
  • Example: regexSplit("apple,orange;banana", "[,;]") // Returns ["apple", "orange", "banana"]
regexReplace(String input, String regex, String replacement)
  • Replace matched patterns with replacement text
  • Example: regexReplace("Call 555-123-4567", "(\\d{3})-(\\d{3})-(\\d{4})", "($1) $2-$3") // Returns "Call (555) 123-4567"
  1. Configuration Options
RegexOptions object with support for:
  • Case insensitivity
  • Multiline mode
  • Global matching
  • Example: getText("USER: John", "user: (\\w+)", {caseInsensitive: true}) // Returns "John"
  1. Pre-built Pattern Library Collection of common regex patterns:
emailPattern() // Returns regex for validating email addresses
phonePattern() // Returns regex for validating phone numbers
urlPattern() // Returns regex for validating URLs
datePattern() // Returns regex for common date formats