Regex Substitute

Name

Regex Substitute

 

Description

Match a regular expression to the value of a parameter and make replacements as dictated by the substitution. The syntax for the regular expression and substitution follows the Python regular expression library and how to guide.

 

Fields

Parameter: The name of the parameter to which the regular expression will be applied.

Regex: The expression used to match a subset of the field name values.

Substitution: The expression used to replace the values of the matched regular expression.

 

Examples

Example A - Replace Student with Learner.

  • Input: Student, Assistant
  • Regex: Student
  • Substitution: Learner
  • Result: Learner, Assistant

 

Example B - Remove a single L if and only if it's the first character in the string.

  • Input: LM_BIOL_1010
  • Regex: (^[L])(.*)
  • Substitution: \2
  • Result: M_BIOL_1010

 

Example C - Replace two periods with underscores.

  • Input: bob.john.smith
  • Regex: ([^\W.]+).([^\W.]+).([^\W.]+)
  • Substitution: \1_\2_\3
  • Result: bob_john_smith

 

Example D - Take an email and remove the domain.

  • Input: bsmith@school.edu
  • Regex: ([^@]+).(.*)
  • Substitution: \1
  • Result: bsmith

 

Example E - Take a course ID of the format Subject-Number-Session.Year.Section separated by hyphens and periods and (1) remove the hyphen between Subject-Number, (2) swap the order of the Year and Session and (3) truncate the sectional information.

  • Input: MATH-1010-FA.2017.CRN14820
  • Regex: ([^\W-]+).([^\W-]+).([^\W.]+).([^\W.]+).([^\W.]+)
  • Substitution: \1\2-\4.\3
  • Result: MATH1010-2017.FA

 

Errors

Errors will occur in the following cases:

  • The regex is malformed (e.g. a bracket or parentheses is left open or unbalanced)
  • The substitution references an invalid group (e.g. \1_\2_\3 where the Regex only has two groups)
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk