Jump to content

How to extract a certain part of a file name


Recommended Posts

Hi,

I have e. g. the following file name:

windowsmedia11-kb936782-x86-deu_315dad75eb6cbc69a4ddaf80cd309ab3a6effdf2.exe

... and I would like to extract the part between "windowsmedia11-kb936782-x86-deu_" and ".exe".

The result is "315dad75eb6cbc69a4ddaf80cd309ab3a6effdf2" (= SHA1). I use the following code to extract:

$xSHA1 = _StringBetween(_xFileName($xArrLst[$k]), "_", ".", -1, -1)

... getting an array ($xSHA1[0]). Is there a way to extract the string with StringRegExp()?

I don't like to return into an array. I just need the plain string.

Working with _ArrayToString() isn't really a choice.

I would like to realize this in one single code line.

Has anyone an idea?

Greets,

-supersonic.

Link to comment
Share on other sites

$t = "windowsmedia11-kb936782-x86-deu_315dad75eb6cbc69a4ddaf80cd309ab3a6effdf2.exe"
ConsoleWrite(StringTrimLeft(StringTrimRight($t, StringLen($t) - StringInStr($t, ".") + 1), StringInStr($t, "_")) & @LF)oÝ÷ Ø@ÈN)²z-­èÆkÊØb±«­¢+Ù
½¹Í½±]ɥѡMÑÉ¥¹IáÁIÁ± ÀÌØíаÅÕ½Ðì ý¤¤ ¸¬¥| ÀäÈí¸¬¤ ÀäÈì¸ÀäÈíܬ¤ÅÕ½Ðì°ÅÕ½ÐìÀÌØìÈÅÕ½Ð줵Àí1

Edited by picaxe
Link to comment
Share on other sites

  • Moderators

Hi,

I have e. g. the following file name:

windowsmedia11-kb936782-x86-deu_315dad75eb6cbc69a4ddaf80cd309ab3a6effdf2.exe

... and I would like to extract the part between "windowsmedia11-kb936782-x86-deu_" and ".exe".

The result is "315dad75eb6cbc69a4ddaf80cd309ab3a6effdf2" (= SHA1). I use the following code to extract:

$xSHA1 = _StringBetween(_xFileName($xArrLst[$k]), "_", ".", -1, -1)

... getting an array ($xSHA1[0]). Is there a way to extract the string with StringRegExp()?

I don't like to return into an array. I just need the plain string.

Working with _ArrayToString() isn't really a choice.

I would like to realize this in one single code line.

Has anyone an idea?

Greets,

-supersonic.

_StringBetween() uses RegEx by default, so you would have to escape the decimal with a slash.

The values are in the elements of the array, you should learn how to work with them because RegEx is only going to return an array unless you use RegExReplace

Array way:

Local $s_str = "windowsmedia11-kb936782-x86-deu_315dad75eb6cbc69a4ddaf80cd309ab3a6effdf2.exe"
Local $a_sre = StringRegExp($s_str, ".*?deu_(.*?)\.", 1)
If Not @error Then MsgBox(64, "Info", $a_sre[0])

Non-Array way:

Local $s_str = "windowsmedia11-kb936782-x86-deu_315dad75eb6cbc69a4ddaf80cd309ab3a6effdf2.exe"
Local $s_sre = StringRegExpReplace($s_str, "(.*?deu_)(.*?)(\..*?\z)", "$2")
If @extended Then MsgBox(64, "Info", $s_sre)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

But I thought the helpfile said it used string manipulation by default?

$iSRE Optional: Toggle for regular string manipulation or StringRegExp search. Default is regular string manipulation.

But feel free to correct me if I'm wrong :)

Cheers,

Brett

Link to comment
Share on other sites

Thank you, works also pretty well! :)

_StringBetween() uses RegEx by default, so you would have to escape the decimal with a slash.

The values are in the elements of the array, you should learn how to work with them because RegEx is only going to return an array unless you use RegExReplace

Array way:

Local $s_str = "windowsmedia11-kb936782-x86-deu_315dad75eb6cbc69a4ddaf80cd309ab3a6effdf2.exe"
Local $a_sre = StringRegExp($s_str, ".*?deu_(.*?)\.", 1)
If Not @error Then MsgBox(64, "Info", $a_sre[0])

Non-Array way:

Local $s_str = "windowsmedia11-kb936782-x86-deu_315dad75eb6cbc69a4ddaf80cd309ab3a6effdf2.exe"
Local $s_sre = StringRegExpReplace($s_str, "(.*?deu_)(.*?)(\..*?\z)", "$2")
If @extended Then MsgBox(64, "Info", $s_sre)
Link to comment
Share on other sites

Thanks, perfect! :)

I'm trying to understand:

Why do you use "\d" instead of "\w"?

And why do you use "\w+" instead of "\w.+"?

As fas as I understood "\d" defines the characters of the string to extract. Right?

"\d" means "Match any digit (0-9)." But the extracted string also contains "a-z" or "A-Z" (lowercase/uppercase possible).

The input string consists of only one single line.

In case of two or more lines, I would like to cut with "\w.+". Right?

Greets,

-supersonic.

$t = "windowsmedia11-kb936782-x86-deu_315dad75eb6cbc69a4ddaf80cd309ab3a6effdf2.exe"
ConsoleWrite(StringTrimLeft(StringTrimRight($t, StringLen($t) - StringInStr($t, ".") + 1), StringInStr($t, "_")) & @LF)oÝ÷ Ø@ÈN)²z-­èÆkÊØb±«­¢+Ù
½¹Í½±]ɥѡMÑÉ¥¹IáÁIÁ± ÀÌØíаÅÕ½Ðì ý¤¤ ¸¬¥| ÀäÈí¸¬¤ ÀäÈì¸ÀäÈíܬ¤ÅÕ½Ðì°ÅÕ½ÐìÀÌØìÈÅÕ½Ð줵Àí1
Link to comment
Share on other sites

how I change the ".exe" in "windowsmedia11-kb936782-x86-deu_315dad75eb6cbc69a4ddaf80cd309ab3a6effdf2.exe" with " ,, ===> 0cd309ab3a6effdf2"exe" to stop read at "exe in my code I wont to use fffsfsf_program"numerico and I wont to read next at line _program" ????? ..

?

Link to comment
Share on other sites

  • Moderators

@sonic

Take a look at some regex tutorials to understand what escape characters and such are.

The regex engine AutoIt uses is called PCRE (Perl Compatible Regular Expressions).

how I change the ".exe" in "windowsmedia11-kb936782-x86-deu_315dad75eb6cbc69a4ddaf80cd309ab3a6effdf2.exe" with " ,, ===> 0cd309ab3a6effdf2"exe" to stop read at "exe in my code I wont to use fffsfsf_program"numerico and I wont to read next at line _program" ????? ..

?

And that makes sense to you at all?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Why do you use "\d" instead of "\w"?

Mistake on my part, which fortunately didn't change the result. SmOke_N's regexp will be better.

In case of two or more lines

Try
$t = "windowsmedia11-kb936782-x86-deu_315dad75eb6cbc69a4ddaf80cd309ab3a6effdf2.exe" & @LF _
    & "windowsmedia11-kb936782-x86-deu_315dad75eb6cbc69akvjdsaf80cd309ab3a6effd.exe" & @CRLF _
    & "windowsmedia11-kb936782-x86-deu_315dad75ertrbc69akvjdsaf80cd309ab3a6effd.exe" & @LF _
    & "windowsmedia11-kb936782-x86-deu_315dad75eb6cbc69akvjdsaf80tyt09ab3a6effd.exe" & @CRLF _
    & "windowsmedia11-kb935582-x86-deu_315dad75eb6cbc69akvjdsaf8077t09ab3a6effd.exe"
ConsoleWrite(StringRegExpReplace($t, "(?i)(.+)_(\w+)\.(\w+)", "$2") & @LF)
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...