pBe Posted January 4, 2011 Posted January 4, 2011 Is Reg.Exp possible for Autocad Mac version? or anybody know the Mac equivalent for Reg.Exp? EDIT: Btw I'm mot Mac user... just curious thats all Quote
irneb Posted January 4, 2011 Posted January 4, 2011 Not sure, but I wouldn't think so. It's loaded from Windows' DLLs for VBScript. How to accomplish that on OSX is beyond me. Maybe someone else has / can give it a try. Or maybe there's some other library to load on Mac (unfortunately though it will have to be loaded through ActiveX, which I think is already a problem on Mac). Quote
Michaels Posted January 5, 2011 Author Posted January 5, 2011 Note: if there are two (2) "-" on your text it will only replace the first one found depending on your needs, we can revise it to loop back.. Also we can add user input for string to find and string to substitute if needed Yes pBe. I noticed that I may have two commas in my text , so how to locate the position of both of them please ? Example . 1234,abcd,567 So the first comma is already known but how to get the location of the second one ? Many thanks Quote
pBe Posted January 5, 2011 Posted January 5, 2011 Michaels See post #10 (by David ) See post #11 (the updated code ) See post #13 (by Lee Mac (Reg.Exp method) and and #20 (the methods as explained by irneb) Quote
irneb Posted January 5, 2011 Posted January 5, 2011 OK, going with pure AutoLisp, assume str contains the string "1234,abcd,567", and ch contains the char to search (i.e. ","): (setq pos 0 ch1 nil ch2 nil) (while (and (not ch2) (<= (setq pos (1+ pos)) (strlen str))) (if (eq ch (substr str pos 1)) (if ch1 (setq ch2 pos) (setq ch1 pos)) ) ) After this ch1 would contain the value 5 and ch2 the value 10. These are the 1-based indexes of the 1st & 2nd commas found in the string. Doing the same with VL extensions: (setq ch1 (vl-string-search ch str)) (setq ch2 (vl-string-search ch str (1+ ch1))) The 2nd line only searches from the position after the 1st found (1+ ch). Note however ch1=4 and ch2=9 now, i.e. the 0-based indexes. So if you now want to extract using substr you'll need to add 1 to each. The RegEx method to just find the positions of all searching characters: (setq found (RX:Search str ch)) ; Returns ((4 . ",") (9 . ",")) (setq ch1 (car (nth 0 found)) ch2 (car (nth 1 found)) ) Again it would be 0-based. After using either of the 3 methods, you need to extract only the portions before and after and combine with the new replacement. Let's say the replacement is saved in variable new. (setq str (strcat (substr str 1 ch1) new (substr str ch2))) ;When using 1-based (setq str (strcat (substr str 1 (1+ ch1)) new (substr str (1+ ch2)))) ;When using 0-based But then why not simply use RegEx to replace the string for you? (setq str (RX:Replace str (strcat ch "[^" ch "]*" ch) (strcat ch new ch))) That one line does everything of all of the above, and it does it a lot faster, with none of the ancillary variables like pos, ch1, ch2 & found as needed by the other methods. Quote
Recommended Posts
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.