RegEx question

Welcome Forums General PowerShell Q&A RegEx question

This topic contains 5 replies, has 4 voices, and was last updated by

 
Participant
1 year ago.

  • Author
    Posts
  • #84508

    Participant
    Points: 0
    Rank: Member

    Hello everyone,

    I have two file formats below that I need to copy to their respective folders 8323434 and 869343434.1.

    What is the RegEx expression for just the number portion (ie. 8323434 and 869343434.1)?

    file 1: SOMEWORD 8323434.pdf
    file 2: SOMEWORD 869343434.1.pdf

    Also, if you could point me to to a good resource on building RegEx, that would be greatly appreciated. Thank you in advance.

  • #84515

    Participant
    Points: 264
    Helping Hand
    Rank: Contributor

    You should try to ask your favorite internet search engine. Regular expression are not secret science. There are tons of information to find in internet ...
    https://en.wikipedia.org/wiki/Regular_expression
    https://www.regular-expressions.info/
    https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
    https://regexr.com/

  • #84526

    Participant
    Points: 10
    Rank: Member
    $folder = Get-ChildItem .\path\to\folder -Filter *.pdf |
    Select-Object fullname,basename
    
    # Get number from filename then create folder
    # New folders are created in files' original location
    foreach ($f in $folder){
    $f.basename -match '\d+.*' | Out-Null
    $newfolder = Join-Path -Path ($f.FullName | Split-Path) -ChildPath $Matches[0]
    If (-NOT(Test-Path $newfolder)) {New-Item $newfolder -ItemType Directory | Out-Null}
    Move-Item -Path $f.FullName -Destination $newfolder
    }
    
  • #84554
    js

    Participant
    Points: 326
    Helping Hand
    Rank: Contributor

    Here's an interactive regex tool: https://regex101.com/

  • #84650

    Participant
    Points: 0
    Rank: Member

    Thank you everyone. Greatly appreciated.

  • #84652

    Participant
    Points: 0
    Rank: Member

    The above script works great. Just a small thing. How do I have it so the files SOMEWORD 8323434.pdf go to a folder 8323434.1 instead of 8323434?

    All files SOMEWORD 8323434.pdf >> folder 8323434.1

    All files SOMEWORD 869343434.1.pdf >> folder 869343434.1

    All files SOMEWORD 565643434.2.pdf >> folder 565643434.2

    so on....

    Many thanks..

The topic ‘RegEx question’ is closed to new replies.