Jump to content

How to split filename on 1st numeric digit?


TXTechie
 Share

Recommended Posts

I will have a folder full of files that look similar to this:

  • RA_CUSTOMER_TRX_ALL_06o3415r7eobtvsr5965b7wrp0ha0200baq.xml
The first part of the files ( words in ALL CAPS separated by underscore characters [ _ ] ) will be different for most every file as will the list of random-looking alpha-numeric characters that make up the second part of the files.

I'm already looping through the folder of files parsing for some other things, but can someone help me with a way to parse out and capture the first part of the filename (RA_CUSTOMER_TRX_ALL_ in my example above - including the final underscore character) into a variable?

I would very much appreciate the assistance!

TX Techie

Link to comment
Share on other sites

Here my version:

$sFile = "RA_CUSTOMER_TRX_ALL_06o3415r7eobtvsr5965b7wrp0ha0200baq.xml"
$1st_part = StringRegExpReplace($sFile, "(?i)(w+_)(.*)", "$1")
MsgBox(0, "Test", $1st_part)

The first part can also have some digits.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

If we can assume the rule is the last _ is always followed by 0-9 then we can dispense with the regex

$str = "RA_CUSTOMER_TRX_ALL_06o3415r7eobtvsr5965b7wrp0ha0200baq.xml"
$count = StringInStr($str,"_",1,-1)
If $count Then
    MsgBox(0x1040,"Test", StringLeft($str,$count))
Else
    MsgBox(0x1010,"Test", "_" & " Not found in string!")
EndIf
Link to comment
Share on other sites

I would do it in yet another way

$sStr = "RA_CUSTOMER_TRX_ALL_06o3415r7eobtvsr5965b7wrp0ha0200baq.xml"
$sPrefix = StringRegExpReplace($sStr, 'A([^d]+).*', "$1")

@UEZ you don't need case insensitive matching (?i), w = [a-zA-Z0-9_]

Yes you are right! I forgot to remove (?i) because my previous expression was different and I needed (?i).

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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