supraspecies Posted July 5, 2016 Share Posted July 5, 2016 A little ashamed I can't figure out something relatively simple, but I'd appreciate the help. I need to trim the string until the first backslash from the right. Like: 'aaa\bbb\ccc' I need to remove "\ccc". The problem here being: 1) "ccc" is a random value 2) "ccc" might have a random number of characters With that said, I can't use either simple replacement string, or subtraction by a number of characters. What would be the best way around it? Thank you. Link to comment Share on other sites More sharing options...
TheDcoder Posted July 5, 2016 Share Posted July 5, 2016 I think I made a function called StringTrimRightUntil... Check my snippets (link in my signature) EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
AspirinJunkie Posted July 5, 2016 Share Posted July 5, 2016 (edited) $s_String = 'aaa\bbb\ccc' $s_NewString = StringLeft($s_String, StringInStr($s_String, "\", 1, -1)-1) MsgBox(0, "The trimmed string", $s_NewString) Edited July 5, 2016 by AspirinJunkie ThomasBennett 1 Link to comment Share on other sites More sharing options...
dmob Posted July 5, 2016 Share Posted July 5, 2016 One way: Local $sString = 'aaa\bbb\ccc' Local $iPos = StringInStr($sString, "\", 0, -1) - 1 Local $sNuString = StringMid($sString, 1, $iPos) ConsoleWrite("$sNuString: " & $sNuString & @CRLF) StringInStr has an option to search from the right. Sure the Regex gurus can whip a one-liner supraspecies 1 Link to comment Share on other sites More sharing options...
TheDcoder Posted July 5, 2016 Share Posted July 5, 2016 1 minute ago, dmob said: Sure the Regex gurus can whip a one-liner Hahaha, so true . But avoid using Regex whenever possible, its ugly. EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
dmob Posted July 5, 2016 Share Posted July 5, 2016 He he he AspirinJunkie beat me to it..... I think I should take some too Link to comment Share on other sites More sharing options...
dmob Posted July 5, 2016 Share Posted July 5, 2016 Just now, TheDcoder said: Hahaha, so true . But avoid using Regex whenever possible, its ugly. Care to elaborate.... I've been aspiring to use Regex wherever possible, I thought it's the better (or grown-up ) way to do things? Link to comment Share on other sites More sharing options...
TheDcoder Posted July 5, 2016 Share Posted July 5, 2016 @dmob Sure, its fast and easy to use Regexes but it is a bad practice in terms of code styling . (My code looks super ugly with them .) EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
AspirinJunkie Posted July 5, 2016 Share Posted July 5, 2016 (edited) So let's give supraspecies the choice: $s_String = 'aaa\bbb\ccc' $s_NewString = StringRegExpReplace($s_String, "\\[^\\]+$", "") MsgBox(0, "The trimmed string", $s_NewString) Edited July 5, 2016 by AspirinJunkie Link to comment Share on other sites More sharing options...
dmob Posted July 5, 2016 Share Posted July 5, 2016 Not so sure about easy.... They give me a headache. Link to comment Share on other sites More sharing options...
TheDcoder Posted July 5, 2016 Share Posted July 5, 2016 @dmob Easy to use, not easy to make! dmob 1 EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
supraspecies Posted July 5, 2016 Author Share Posted July 5, 2016 Thank you all very much for your replies. So many different suggestions! The problem is solved. Link to comment Share on other sites More sharing options...
dmob Posted July 5, 2016 Share Posted July 5, 2016 Just now, AspirinJunkie said: So let's give supraspecies the choice: $s_String = 'aaa\bbb\ccc' $s_NewString = StringRegExpReplace($s_String, "[^\\]+$", "") MsgBox(0, "The trimmed string", $s_NewString) Your regex returns a trailing slash... The OP said 30 minutes ago, supraspecies said: I need to remove "\ccc". Link to comment Share on other sites More sharing options...
TheDcoder Posted July 5, 2016 Share Posted July 5, 2016 @supraspecies Just out of curiosity, which solution did you use? EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
AspirinJunkie Posted July 5, 2016 Share Posted July 5, 2016 Ah ok. Seems that the eyes doesn't benefit from Aspirin ... I edit the two solutions. dmob 1 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 5, 2016 Moderators Share Posted July 5, 2016 TheDcoder, Quote Sure, its fast and easy to use Regexes but it is a bad practice in terms of code styling Wrong on both counts: Regexes are not easy (unless your brain is wired differently from the majority of coders), but can be super efficient. Using them is most certainly not "bad practice" of any form. As George always used to point out, the real difficulty with Regexes is learning when not to use them when a simpler solution will suffice. This case is one where a Regex is the sensible answer: ConsoleWrite(StringRegExpReplace("aaa\bbb\ccc", "(^.*\\)(.*)", "$1") & @CRLF) M23 Skysnake 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
TheDcoder Posted July 5, 2016 Share Posted July 5, 2016 I disagree @Melba23, Regexes look ugly and are bad for code styling in my opinion. btw, "easy" as in use, not in making them (already stated in post #11) EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
jchd Posted July 5, 2016 Share Posted July 5, 2016 @TheDcoder, Let me strongly disagree in turn: it isn't because a language syntax or writing system looks ugly to some people that it's inherently bad or that it can't be a good vehicle for powerful and subtle concepts (or programs). मानक हिन्दी looks like graffiti to most westerners, yet it's the 4th most used language on Earth as you surely know. APL looks terrible to non-APLists but it's unprecedently concise and powerful; APL is currently regaining interest (as APL2, A+ or J) in several domains. Perl makes a heavy use of regexps but has been the workhorse of the web and many other use cases, PHP without regexps is like a spider with no legs, and the list goes on. Almost all powerful generic languages offer SRE support and for a reason, else noone would bother wasting energy, time and money to include such support if it didn't have a significant usefulness. Your opinion seems based on your own laziness or unwillingness to approach SREs with a technically open mindframe. kcvinu and Skysnake 2 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
TheDcoder Posted July 5, 2016 Share Posted July 5, 2016 @jchd I take back my words . P.S 5 minutes ago, jchd said: मानक हिन्दी Translated into English: "Believe Hindi" EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
jchd Posted July 5, 2016 Share Posted July 5, 2016 It's the name for the official Modern Standard Hindi language in whole India in Devanagari script, even if some areas favor other languages/dialects and scripts. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now