Jump to content

StringTrimRight until a certain character


Recommended Posts

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

1 minute ago, dmob said:

Sure the Regex gurus can whip a one-liner

Hahaha, so true :D. 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

Just now, TheDcoder said:

Hahaha, so true :D. 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 :D) way to do things?

Link to comment
Share on other sites

@dmob Sure, its fast and easy to use Regexes but it is a bad practice in terms of code styling :D. (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

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

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

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

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 here
RegExp tutorial: enough to get started
PCRE 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

@jchd I take back my words waving-white-flag.gif.

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

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 here
RegExp tutorial: enough to get started
PCRE 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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...