CharacterArray.st
changeset 24601 5da158142cda
parent 24560 8a25fd9cf698
child 24684 98b15ae35389
--- a/CharacterArray.st	Sat Aug 24 19:20:59 2019 +0200
+++ b/CharacterArray.st	Sat Aug 24 19:21:11 2019 +0200
@@ -6465,6 +6465,31 @@
 ! !
 
 
+!CharacterArray methodsFor:'matching - regex'!
+
+subExpressionsInRegex:rxString caseSensitive:caseSensitive
+    "return the collection of matching regex-subexpressions.
+     Subexpressions are sub-patterns enclosed in parentheses in the regex.
+     substrings in the receiver.
+     Returns nil, if there is no match.
+     Use this to extract particular parts from the receiver.
+     Refer to `documentation' protocol of RxParser class for details."
+
+     |matcher|
+
+     matcher := Regex::RxMatcher new initializeFromString:rxString ignoreCase:caseSensitive not.
+     (matcher matches:self) ifTrue:[
+        ^ matcher subexpressions
+     ].
+     "/ no match
+     ^ nil.
+
+    "
+     'I am Tim Dalton' subExpressionsInRegex:'I am (.+)' caseSensitive:false   
+     'i am Boris Johnson' subExpressionsInRegex:'I am (.+)' caseSensitive:false  
+     'you are Boris Johnson' subExpressionsInRegex:'(i|you) (am|are) (.+)' caseSensitive:false  
+    "
+! !
 
 !CharacterArray methodsFor:'padded copying'!