140 likes | 153 Views
Learn about the history and usage of regular expressions in programming languages. Understand literal character search, special characters like period and quantifiers, and how to use parentheses, square brackets, and shorthand character classes. Explore greedy matches and advanced techniques for efficient find and replace operations.
E N D
ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS CHRISTOPHER ZACK|OMNIUPDATE
Agenda • ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS • Find & Replace • History of Regular Expressions • Literal Character Search • Special Characters • Period • Quantifiers • Parentheses • Square Brackets • Shorthand Character Classes • Greedy Matches
Find & Replace • ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS • Location: Content > Find and Replace • Administrators Only (User Level 10) • Searches a single site • Adjust scope to limit searchable content • Literal Text or Regular Expression
Find & Replace • ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS • Find • Simple search with results list. • Preview Replace • Safe multi-step process • Perform ‘sample’ find/replace and display results list. • Select pages from results to perform the actual find/replace operation. • Publish selected results (optional).
History of Regular Expressions • ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS • Regular Expression • A pattern that describes a certain amount of text. • The concept arose in the 1950s when the American mathematician Stephen Cole description of a regular language. • Now used in almost every major programming language.
Literal Character Search • ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS • Literal Text Matches • Most characters match exactly themselves (beware of whitespace) • Case Sensitive • Great way to get a general idea of how many items will at least need to be captured if you need to generate a regular expression “find.”
Special Characters • ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS Symbol characters that have special purpose: \ ^ $ . | ? * ( ) [ ] { } To match as literal characters, you must escape them by adding a backslash “\” in front of said special character.
Special Characters: Period • ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS • The period character is the “wildcard” character • Will match any character except a newline (\n)
Special Characters: Quantifiers • ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS • Symbol characters that define how many of the previous character(s) to match • ? (0 or 1) • * (0 or more) • + (1 or more) • Use curly brackets to indicate an exact number or range • {3} (Exactly 3) • {3,} (3 or more) • {3,5} (3, 4, or 5) • Only modifies the previous character (or group)
Special Characters: Parenthesis • ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS • Capture Groups • Encapsulate a character sequence using parentheses: “(…)” • Add a quantifier to affect the whole group • Replace • In the “replace field,” refer to your groups using the dollar sign and then the group number: $# • Count the opening parenthesis characters, “(“, to determine the correct #
Special Characters: Square Brackets • ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS • Character Sets • Characters encased inside square brackets define all possible matches for a single text character: [abc] • A quantifier place directly after the set will affect the whole character set • Placing a “-” hyphen between characters indicates a range • Placing a “^” caret as the first item in the set creates a negative pattern • Quantifier characters become literal matches: ? + * { } • Placing a “.” period becomes literal match
Shorthand Character Classes • ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS • Certain characters can reference a range of characters when escaped by a backslash “\” • Examples: • “\d” matches all digit characters: [0-9] • “\w” matches all word characters: [A-Za-z0-9] • “\s” matches all whitespace characters (including line breaks) • Using the capital letter will inverse the match • “\S” matches all non-space characters: [^\s]
Greedy Matches • ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS • When using quantifiers, a careless (or purposeful) pattern could match beyond an expected result • Applying an extra “?” question mark character after the initial quantifier, to make the pattern stop at the first successful match • [\s\S]*?
THANK YOU! PLEASE SHARE YOUR FEEDBACK: WWW.SLIDO.COM#OUTC19