Jump to content

Recommended Posts

Posted

hi.all

if I want to get/override some special substring in a big string.what is the most efficient /easy to do this.

e.g.

I have a string {\H1.1429x;φ}{\H0.7x;\S+0.1^-0.2;\H1.4286x;\C1;(small)}

 

then I want to get the blue part substring.or override the blue part with "replace",what

should I do?thanks.

Posted

hi,Lee

after some study,I use the following codes to get the value "+0.1" and "-0.2",but it failed ,any comments??

(defun c:tt(/)
 	(vl-load-com)
(setq regex (vlax-create-object "Vbscript.RegExp"))
(vlax-put-property regex "IgnoreCase" 0)
(vlax-put-property regex "Global" 1)
(vlax-put-property regex "Pattern" "\\\\S(.*)(\\^|#|\\\\)(.*);")
(setq string "{\H1.1429x;φ}<>{\H0.7x;\S+0.1^-0.2;\H1.4286x;\C1;(small)}")
(setq result (vlax-invoke-method regex "Execute" string))
(vlax-for x result
	(setq match (vlax-get x 'Value)
			idx  (valx-get x 'FirstIndex)
			lst (cons (list match idx) lst)
	)
)
lst
)

Posted

ok,here are woked codes.

but it return the matched string \\S+0.1^-0.2;

if I just want to get "+0.1" and "-0.2",what should I do?

 

(defun c:tt(/ regex lst)
 	(vl-load-com)
(setq regex (vlax-create-object "Vbscript.RegExp"))
(vlax-put-property regex "IgnoreCase" 0)
(vlax-put-property regex "Global" 1)
(vlax-put-property regex "Pattern" "\\\\S(.[^;]*)(\\^)(.[^;]*);")
(setq string "{\H1.1429x;φ}<>{\H0.7x;\\S+0.1^-0.2;\H1.4286x;\C1;(small)}")
(setq result (vlax-invoke-method regex "Execute" string))
(vlax-for x result
	(setq match (vlax-get x 'Value)
			idx  (vlax-get x 'FirstIndex)
			lst (cons (list match idx) lst)
	)
)
lst
(vlax-release-object regex)
 	
)

Posted

I'm not too good with RegularExpressions, but maybe this is closer?

 

(defun c:tt ( / *error* regex str )
 (vl-load-com)

 (setq str "{\H1.1429x;?}<>{\H0.7x;\\S+0.1^-0.2;\H1.4286x;\C1;(small)}")

 (defun *error* ( msg )
   (LM:ReleaseObject regex)
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ)
 )

 (setq regex (vlax-create-object "VBScript.RegExp"))

 (setq str (caar (LM:RegExExecute regex "\\\\S.[^;]*\\^.[^;]*;" str)))

 (print (LM:RegExExecute regex "[^S]*\\^[^;]*" str))
 
 (LM:ReleaseObject regex)
 (princ)
)


(defun LM:RegExExecute ( regex pat str / l )
 ;; © Lee Mac 2010
 (mapcar
   (function
     (lambda ( prop value ) (vlax-put-property regex prop value))
   )
  '(pattern global ignorecase) (list pat actrue acfalse)
 )
 (vlax-for x (vlax-invoke regex 'execute str)
   (setq l (cons (list (vlax-get x 'value) (vlax-get x 'firstindex)) l))
 )
 (reverse l)
)

;;------------------=={ Release Object }==--------------------;;
;;                                                            ;;
;;  Releases a VLA Object from memory via plentiful error     ;;
;;  trapping                                                  ;;
;;------------------------------------------------------------;;
;;  Author: Lee McDonnell, 2010                               ;;
;;                                                            ;;
;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;
;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  obj - VLA Object to be released from memory               ;;
;;------------------------------------------------------------;;
;;  Returns:  T if Object Released, else nil                  ;;
;;------------------------------------------------------------;;

(defun LM:ReleaseObject ( obj )
 (vl-load-com)
 ;; © Lee Mac 2010
 (and obj (eq 'VLA-OBJECT (type obj)) (not (vlax-object-released-p obj))
   (not
     (vl-catch-all-error-p
       (vl-catch-all-apply
         (function vlax-release-object) (list obj)
       )
     )
   )
 )
)

Posted

the final one.:)

(defun c:tt ( / *error* regex str )
 (vl-load-com)

 (setq str "{\H1.1429x;?}<>{\H0.7x;\\S+0.1^-0.2;\H1.4286x;\C1;(small)}")

 (defun *error* ( msg )
   (LM:ReleaseObject regex)
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ)
 )

 (setq regex (vlax-create-object "VBScript.RegExp"))

 (setq str (caar (LM:RegExExecute regex "\\\\S.[^;]*\\^.[^;]*;" str)))

 (setq str (caar (LM:RegExExecute regex "[^S]*\\^[^;]*" str)))
 (print (LM:RegExExecute regex "[^\\^]+" str))
 (LM:ReleaseObject regex)
 (princ)
)


(defun LM:RegExExecute ( regex pat str / l )
 ;; ? Lee Mac 2010
 (mapcar
   (function
     (lambda ( prop value ) (vlax-put-property regex prop value))
   )
  '(pattern global ignorecase) (list pat actrue acfalse)
 )
 (vlax-for x (vlax-invoke regex 'execute str)
   (setq l (cons (list (vlax-get x 'value) (vlax-get x 'firstindex)) l))
 )
 (reverse l)
)

;;------------------=={ Release Object }==--------------------;;
;;                                                            ;;
;;  Releases a VLA Object from memory via plentiful error     ;;
;;  trapping                                                  ;;
;;------------------------------------------------------------;;
;;  Author: Lee McDonnell, 2010                               ;;
;;                                                            ;;
;;  Copyright ? 2010 by Lee McDonnell, All Rights Reserved.   ;;
;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  obj - VLA Object to be released from memory               ;;
;;------------------------------------------------------------;;
;;  Returns:  T if Object Released, else nil                  ;;
;;------------------------------------------------------------;;

(defun LM:ReleaseObject ( obj )
 (vl-load-com)
 ;; ? Lee Mac 2010
 (and obj (eq 'VLA-OBJECT (type obj)) (not (vlax-object-released-p obj))
   (not
     (vl-catch-all-error-p
       (vl-catch-all-apply
         (function vlax-release-object) (list obj)
       )
     )
   )
 )
)

Posted

Glad you got there in the end :)

 

It probably needs a lot more error trapping though...

Posted

hi,Lee:

I found another better way to get the value.

codes as follows.

 

(defun c:tt(/ regex lst)
 	(vl-load-com)
(setq regex (vlax-create-object "Vbscript.RegExp"))
(vlax-put-property regex "IgnoreCase" 0)
(vlax-put-property regex "Global" 1)
;
(vlax-put-property regex "Pattern" ".*\\\\S(.[^;]*)[\\^](.[^;]*);.*")
(setq string "<>{\H0.7x;\\S+0.3^+0.5;}")
(setq tp (vlax-invoke-method regex "Replace" string "$1"))
 	(setq tm (vlax-invoke-method regex "Replace" string "$2"))
 	(princ tp)
   (princ tm)
(vlax-release-object regex)
 	
)

Posted

base on the same story.

but it failed to the below codes.any idea on this???

(defun c:tt(/ regex lst)
 	(vl-load-com)
(setq regex (vlax-create-object "Vbscript.RegExp"))
(vlax-put-property regex "IgnoreCase" 0)
(vlax-put-property regex "Global" 1)
;
(vlax-put-property regex "Pattern" ".*%%P(\d*[\\\\.]?\d*).*")
(setq string "<>%%P0.05")
(setq tp (vlax-invoke-method regex "Replace" string "$1"))
 	;(setq tm (vlax-invoke-method regex "Replace" string "$2"))
 	(princ tp)
   ;(princ tm)
(vlax-release-object regex)
 	
)

Posted

Nice idea to use the entries from matches collection :)

 

To fix your issue:

 

".*%%P(\\d*[\\\\.]?\\d*).*"

 

:)

Posted

See how to use groups:

quickly tested though)

(vl-load-com)
(defun C:demo  (/ elist en string regex result)
 (setq string (cdr
  (assoc 1
  (setq elist (entget
         (setq en (car (entsel "\n\t>> Select dimension with tolerance: "))))))))
 (setq regex (vlax-create-object "Vbscript.RegExp"))
 (vlax-put-property regex "IgnoreCase" 0)
 (vlax-put-property regex "Global" 1)
 (vlax-put-property
   regex
   "Pattern"
   "(.\\\\[s])(.*?)(\\^)(.*?);(.*?)")
 (setq result (vlax-invoke-method
  regex
  "Replace"
  string
  (strcat "$1" "+0.00125" "$3" "-0.0025" "$5;")));<--build replacement here
 (vlax-release-object regex)
 (entmod (subst (cons 1 result) (assoc 1 elist) elist))
 (entupd en)
 (princ)
 )

 

~'J'~

  • Like 1
Posted

I believe Fixo is demonstrating the idea of retrieving items from the Matches Collection when using a 'remembered match', i.e.:

 

[color=blue][b]Original: [/b][/color]
"[color=green][b]a[/b][b][color=red]b[/color][/b][/color][color=darkorange][b]c[/b][/color] abc abc" 

[color=blue][b]Pattern:[/b][/color]
"[b][color=green](\\S)[/color][color=red](\\S)[/color][/b][b][color=darkorange](\\S)[/color][/b]" 
[color=blue][b]
Replace with:[/b][/color]
"[b][color=darkorange]$3[/color][color=red]$2[/color][color=green]$1[/color][/b]" 
[color=blue][b]
Result:[/b][/color]
"[b][color=darkorange]c[/color][color=red]b[/color][color=green]a[/color][/b] cba cba" 
[font=monospace]

[/font]

Posted
I believe Fixo is demonstrating the idea of retrieving items from the Matches Collection when using a 'remembered match', i.e.:

 

[color=blue][b]Original: [/b][/color]
"[color=green][b]a[/b][b][color=red]b[/color][/b][/color][color=darkorange][b]c[/b][/color] abc abc" 

[color=blue][b]Pattern:[/b][/color]
"[b][color=green](\\S)[/color][color=red](\\S)[/color][/b][b][color=darkorange](\\S)[/color][/b]" 

[b][color=blue]Replace with:[/color][/b]
"[b][color=darkorange]$3[/color][color=red]$2[/color][color=green]$1[/color][/b]" 

[b][color=blue]Result:[/color][/b]
"[b][color=darkorange]c[/color][color=red]b[/color][color=green]a[/color][/b] cba cba" 

Yes, it's the usual way to substitute substring :)

 

~'J'~

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...