Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Strip trailing \\P from multline string?

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
RandyBenson
789 Views, 7 Replies

Strip trailing \\P from multline string?

The following line of vlisp strips out trailing \\P from a multiline string, but in the case where the string ends in "P",

where it also strips out the trailing "P", e.g.  "FS EP" goes to "FS E".

 

(setq desc (vl-string-right-trim "\\P" desc))

 

Is there any other way to strip out line feed characters from Mtext or multiline attributes?

TIA,

Randy

 

7 REPLIES 7
Message 2 of 8
Moshe-A
in reply to: RandyBenson

@RandyBenson  hi,

 

try this:

 

(vl-string-subst "" "\\P" "FS EP")

 

moshe

 

Message 3 of 8
Kent1Cooper
in reply to: Moshe-A


@Moshe-A wrote:

....

(vl-string-subst "" "\\P" "FS EP")


 

Careful....  If the multi-line string content includes any other line-feed characters,  in addition to the trailing one, that will remove only the first one, and leave the one at the end  [and any others after the first].  If the intent is to remove all  of them, you can do something like this:

 

(while
  (wcmatch YourStringContent "*\\P*"); still any remaining?
  (setq YourStringContent (vl-string-subst "" "\\P" YourStringContent)); remove one
); while

But if there might be more than one, and you want to remove only a trailing one, that would be trickier [but should be achievable].  For instance, if "trailing" means the line-feed is only and always at the very end  [i.e. not for example followed by any space(s)], try something like this:

(if (wcmatch YourStringContent "*\\P"); ends with it
  (setq YourStringContent
    (substr YourStringContent 1 (- (strlen YourStringContent) 2)); remove end
  ); setq
); if 

 

Kent Cooper, AIA
Message 4 of 8
dlanorh
in reply to: RandyBenson

Another

 

(if (vl-string-search "\\P" str (- (strlen str) 4)) (setq str (vl-string-subst "" "\\P" str (- (strlen str) 4))))

I am not one of the robots you're looking for

Message 5 of 8
_gile
in reply to: RandyBenson

Hi,

 

Another way would be using regular expressions.

 

;;--------------------------------------------------------------------------------------------------------;;
;;                                          Regular Expressions                                           ;;
;;--------------------------------------------------------------------------------------------------------;;
;;                                                                                                        ;;
;; LISP functions to emulate properties and methods of the vbscript RegExp type.                          ;;
;;                                                                                                        ;;
;; Gilles Chanteau                                                                                       ;;
;;--------------------------------------------------------------------------------------------------------;;

(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)
)

;; RegExpExecute
;; Returns the list of matches with the pattern found in the string.
;; Each match is returned as a sub-list containing:
;; - the match value
;; - the index of the first character (0 based)
;; - a list of sub-groups.
;;
;; Arguments
;; string     : String in which the pattern is searched.
;; 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
;; (RegExpExecute "foo bar baz" "ba" nil nil)               ; => (("ba" 4 nil))
;; (RegexpExecute "12B 4bis" "([0-9]+)([A-Z]+)" T T)        ; => (("12B" 0 ("12" "B")) ("4bis" 4 ("4" "bis")))
;; (RegexpExecute "-12 25.4" "(-?\\d+(?:\\.\\d+)?)" nil T)  ; => (("-12" 0 ("-12")) ("25.4" 4 ("25.4")))

(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-for submatch (vlax-get match 'SubMatches)
	   (if submatch
	     (setq sublst (cons submatch sublst))
	   )
	 )
       )
    )
    (setq lst (cons (list (vlax-get match 'Value)
			  (vlax-get match 'FirstIndex)
			  (reverse sublst)
		    )
		    lst
	      )
    )
  )
  (reverse lst)
)

;; 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)
)

Using:

(setq desc (RegexpReplace desc "\\\\P" "" nil T))


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 8
RandyBenson
in reply to: _gile

I owe you a full glass of your favorite adult beverage for the most provocatively further-study-inducing post I've seen in a Sunth of Mondays!

Thank you.

Message 7 of 8
RandyBenson
in reply to: dlanorh

Thanks, dlanorh, that works; the main thing I needed was the wcmatch test, what I finally used was this:

(if (wcmatch desc "*\\P")
  (setq desc (vl-string-right-trim "\\P" desc))
)

Message 8 of 8
RandyBenson
in reply to: Kent1Cooper

Hi Kent:

Thanks -- the wcmatch test for trailing instances is what I needed to do.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost