Jump to content
Sign in to follow this  
TXTechie

How to split filename on 1st numeric digit?

Recommended Posts

TXTechie

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

Share this post


Link to post
Share on other sites
GMK

$sString = "RA_CUSTOMER_TRX_ALL_06o3415r7eobtvsr5965b7wrp0ha0200baq.xml"
$sPrefix = StringRegExpReplace($sString, "(.*?)d.*xml", "1", 0)

Edited by GMK

Share this post


Link to post
Share on other sites
TXTechie

Thank you very much, GMK! That did the trick!

Share this post


Link to post
Share on other sites
UEZ

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
Robjong

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_]

Share this post


Link to post
Share on other sites
Malkey

And the other view is "keep everything you don't catch" or "throw out everything you catch (match)".

$sFile = "RA_CUSTOMER_TRX_ALL_06o3415r7eobtvsr5965b7wrp0ha0200baq.xml"
$1st_part = StringRegExpReplace($sFile, "d.*xml", "")
MsgBox(0, "Test", $1st_part)

Share this post


Link to post
Share on other sites
MilesAhead

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

Share this post


Link to post
Share on other sites
UEZ

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
Robjong

I suspected as much, it's usually copy/paste related :oops:

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
Sign in to follow this  

×