Jump to content

How to create a function that support Regex


Recommended Posts

Posted

I need a function that can replace all  sub old string with sub new str in the source string  like the java String's replaceAll function,

how to make it?

Posted
(vl-load-com)

;; RegExpSet
;; Returns the current VBScript.RegExp instance after defining its properties.
;;
;; Arguments
;; pattern		: Pattern to search.
;; ignoreCase : If non nil, the search is done ignoring the case.
;; global		 : If non nil, search all occurences of the pattern;
;;							if nil, only searches the first occurence.

(defun RegExpSet (pattern ignoreCase global / regex)
	(setq regex
				 (cond
					 ((vl-bb-ref '*regexp*))
					 ((vl-bb-set '*regexp* (vlax-create-object "VBScript.RegExp")))
				 )
	)
	(vlax-put regex 'Pattern pattern)
	(if ignoreCase
		(vlax-put regex 'IgnoreCase acTrue)
		(vlax-put regex 'IgnoreCase acFalse)
	)
	(if global
		(vlax-put regex 'Global acTrue)
		(vlax-put regex 'Global acFalse)
	)
	regex
)

;; RegexpTest
;; Return T if a match with the pattern is found in the string; otherwise, nil.
;;
;; Arguments
;; string		 : String in which the pattern is searched.
;; pattern		: Pattern to search.
;; ignoreCase : If non nil, the search is done ignoring the case.
;;
;; Examples :
;; (RegexpTest "foo bar" "Ba" nil)	; => nil
;; (RegexpTest "foo bar" "Ba" T)		; => T
;; (RegExpTest "42C" "[0-9]+" nil)	; => T

(defun RegexpTest (string pattern ignoreCase)
	(= (vlax-invoke (RegExpSet pattern ignoreCase nil) 'Test string) -1)
)


;; RegExpReplace
;; Returns the string after replacing matches with the pattern
;;
;; Arguments
;; string		 : String in which the pattern is searched.
;; pattern		: Pattern to search.
;; newStr		 : replacement string.
;; pattern		: Pattern to search.
;; ignoreCase : If non nil, the search is done ignoring the case.
;; global		 : If non nil, search all occurences of the pattern;
;;							if nil, only searches the first occurence.
;;
;; Examples :
;; (RegexpReplace "foo bar baz" "a" "oo" nil T)									; => "foo boor booz"
;; (RegexpReplace "foo bar baz" "(\\w)\\w(\\w)" "$1_$2" nil T)	 ; => "f_o b_r b_z"
;; (RegexpReplace "$ 3.25" "\\$ (\\d+(\\.\\d+)?)" "$1 €" nil T)	; => "3.25 €"

(defun RegExpReplace (string pattern newStr ignoreCase global)
	(vlax-invoke (RegExpSet pattern ignoreCase global) 'Replace string newStr)
)


;把替换字串的纯文本转义字符转义
(defun escapeMatchString ( str )
	(RegExpReplace str "([\\{\\}\\[\\]\\^\\(\\)\\$\\+\\.\\*\\?])" "\\$1" t t)
)



(defun RegExpExecute (string pattern ignoreCase global / sublst lst)
	(vlax-for match (vlax-invoke (RegExpSet pattern ignoreCase global) 'Execute string)
		(setq sublst nil)
		(vl-catch-all-apply
			'(lambda ()
	 (vlax-map-collection	(vlax-get match 'SubMatches)
		 ;(if submatch
		 ;	(setq sublst (cons submatch sublst))
		 ;)
		 '(lambda(str)(setq sublst (cons str sublst)))
	 )
			 )
		)
		(setq lst (cons (list (vlax-get match 'Value)
				(vlax-get match 'FirstIndex)
				(reverse sublst)
				)
				lst
				)
		)
	)
	(reverse lst)
)

Here is a vba regex wrapper

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...