supraspecies

StringTrimRight until a certain character

31 posts in this topic

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.

Share this post


Link to post
Share on other sites



I think I made a function called StringTrimRightUntil... Check my snippets (link in my signature)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#3 ·  Posted (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 by AspirinJunkie

Share this post


Link to post
Share on other sites

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

 

1 person likes this

Share this post


Link to post
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.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

He he he AspirinJunkie beat me to it..... I think I should take some too :D

Share this post


Link to post
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?

Share this post


Link to post
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 :'(.)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#9 ·  Posted (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 by AspirinJunkie

Share this post


Link to post
Share on other sites

Not so sure about easy.... They give me a headache.

Share this post


Link to post
Share on other sites

@dmob Easy to use, not easy to make! :P

1 person likes this

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

Thank you all very much for your replies. So many different suggestions! The problem is solved. :)

Share this post


Link to post
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".

 

Share this post


Link to post
Share on other sites

@supraspecies Just out of curiosity, which solution did you use? :huh:


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

Ah ok. Seems that the eyes doesn't benefit from Aspirin ...

I edit the two solutions.

1 person likes this

Share this post


Link to post
Share on other sites

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

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
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)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
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.

2 people like this

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)

Share this post


Link to post
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"


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
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)

Share this post


Link to post
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