Jump to content
Terenz

Regex get month and year

Recommended Posts

Hello,

I need to match in regex if a filename have month and year. Example

my_business_03_2020

03 my business 2020

And so on. So i need to match if there are two digit consecutive and four digit consecutive and extract from the file name. I need both, if exist one of the two is considerated "error" for me

Thanks ;)


Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites
Posted (edited)

Can this help

$sTestString="my_business_032020"
$btest=StringRegExp($sTestString,".*\d\d.*\d\d\d\d")
MsgBox(0,"Test",($btest ? "There is" : "There is not") & " date")

You can look at regex101.com for explanation

Edited by ahmet

Share this post


Link to post
Share on other sites
Posted (edited)

Hi ahmet. Thanks for your help

I need to get the month and the date from the file

[0]=03

[1]=2020

I have try to change the RegEx flag to 3 but not work, return all the string

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites
$sTestString="my_business_032020"
$btest=StringRegExp($sTestString,".*(\d\d).*(\d\d\d\d)",1)
if IsArray($btest) Then
    $month = $btest[0]
    $year = $btest[1]
    MsgBox(0,"Test", "Month: " & $month & @CRLF & "Year: " & $year)
Else
    ; error
EndIf

Based on the example from ahmet :)


It's my job to comfort the disturbed and to disturb the comfortable.
My Projects: Profiler, MakeSFX, UserInfo, Simple Robocopy Progressbar

Share this post


Link to post
Share on other sites

Maybe this ?

#include <Constants.au3>
#include <Array.au3>

Local $aTxt = ["my_business_03_2020", "03 my business 2020"]

For $sTxt in $aTxt
  $arr = StringRegExp ($sTxt, "\D?(\d{2}|\d{4})(?=\D|$)", 3)
  _ArrayDisplay ($arr)
Next

 

Share this post


Link to post
Share on other sites

Cool! Thanks!


Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...