Home
Join

20 Replies

  • like so?

    Powershell
    ^RP\.[A-Za-z]*\.[A-Za-z]*[A-Za-z0-9]
    
    Was this post helpful? thumb_up thumb_down
  • Neally, Thank you for your reply.  I will go test it and get back to you.  I forgot to mention I am using PowerShell.  

    Was this post helpful? thumb_up thumb_down
  • Text
    ^RP\.[A-Za-z]+\.(?:(?=[a-zA-Z\d])[A-Za-z]*\d*)
    

    See Regex Demo

    Was this post helpful? thumb_up thumb_down
  • Bojan, Thank you for your reply.  It works.  I cannot make it fail.  There is more than one way to do something.   Thank you for the regex101.com  reference.  I am beginning to understand how the site works. 

    Neally, I have stress tested yours too.  Yours is perfect too. 

    Thanks to both of you. 

    Now, I know how to test the input of my powershell script to see if the first part of the string is:  RP.

    Like this:   

    -cmatch  ‘^RP\.’

     I would also like to isolate at test for only the First Name  because I want to send an error message that tells the operator what is wrong: “First Name May Only Have Alpha”  

    With input like this: RP.7Jane.Doe 

    I am trying to devise some kind of match would ignore everything else and return a false because  no numbers are allowed in the first name.   

     I read that .+  means any character.

    So I have tried  various things like this but I am not getting any good results yet:

    ^.+\.[A-Za-z]*\.[A-Za-z]*[.+]

    Thanks in advance for your additional help on this. 


    Was this post helpful? thumb_up thumb_down
  • Just to let you know I am trying on my own,  I think this may work:  ^.+\.[A-Za-z]*\..+

    I will test some more and let you know.

    Was this post helpful? thumb_up thumb_down
  • When I checked Neally's expression, it failed to select the second number at the end. So it's depending on the purpose of the expression, if that is good enough or not.

    Because it did 'select' only the first number at the end, I started trying to figure out how to get also the second number included.

    If it's only about figuring out if a string matches the 'basic' criteria, than Neally's expression will do the job. If you want to do some 'search&replace' / formatting, than you would want all the numbers in the end to be included.

    Was this post helpful? thumb_up thumb_down
  • Bojan Zajc wrote:

    When I checked Neally's expression, it failed to select the second number at the end. So it's depending on the purpose of the expression, if that is good enough or not.

    Because it did 'select' only the first number at the end, I started trying to figure out how to get also the second number included.

    If it's only about figuring out if a string matches the 'basic' criteria, than Neally's expression will do the job. If you want to do some 'search&replace' / formatting, than you would want all the numbers in the end to be included.

    How did you test it? What you linked tested ECMA standards not powershell.

    ez test, actually run it in powershell...

    Powershell
    clear
    $list = 
    @"
    RP.Lizzie.Borden
    RP.Ma.Barker
    RP.Atilla.Hun
    RP.Ted.Bundy7
    RP.Al.Capone29
    RP.J.J
    RP.J.1
    RP.J.22
    RP.Lizzie7.Borden
    RP.Lizzie.
    RP.Lizzie
    "@ -split "`r`n"
    
    foreach($line in $list){
        if($line -match "^RP\.[A-Za-z]*\.[A-Za-z]*[A-Za-z0-9]"){
            $line
        }
    }
    
    Powershell
    RP.Lizzie.Borden
    RP.Ma.Barker
    RP.Atilla.Hun
    RP.Ted.Bundy7
    RP.Al.Capone29
    RP.J.J
    RP.J.1
    RP.J.22
    
    Was this post helpful? thumb_up thumb_down
  • Neally wrote:

    How did you test it? What you linked tested ECMA standards not powershell.

    ez test, actually run it in powershell...

     
    Actually it is exactly as I said - your expression just checks a line if there is something that matches the minimal criteria - but ignores the rest of the numbers once it has got one number.
    Depending of the purpose of the use for the expression, this may be good enough - or not good enough.
    Good enough would be, if you are simply checking if the criteria is matched.
    Not good enough, when you want to use the matched string in a replacement or print 'something' with it.
    Powershell
    clear
    $list = 
    @"
    RP.Lizzie.Borden
    RP.Ma.Barker
    RP.Atilla.Hun
    RP.Ted.Bundy7
    RP.Al.Capone29
    RP.J.J
    RP.J.1
    RP.J.22
    RP.Lizzie7.Borden
    RP.Lizzie.
    RP.Lizzie
    "@ -split "`r`n"
    
    
    echo "Matching with ^RP\.[A-Za-z]*\.[A-Za-z]*[A-Za-z0-9] :"
    echo "_______________________________________________________________"
    echo ""
    
    foreach($line in $list){
        if($line -match "^RP\.[A-Za-z]*\.[A-Za-z]*[A-Za-z0-9]"){
            $My_Match=$matches[0]
            echo "The name $My_Match is o.k.?"
        }
    }
    
    echo ""
    echo ""
    echo ""
    echo "Matching with ^RP\.[A-Za-z]+\.(?:(?=[a-zA-Z\d])[A-Za-z]*\d*) : "
    echo "_______________________________________________________________"
    echo ""
    
    foreach($line in $list){
        if($line -match "^RP\.[A-Za-z]+\.(?:(?=[a-zA-Z\d])[A-Za-z]*\d*)"){
            $My_Match=$matches[0]
            echo "The name $My_Match is o.k.?"
        }
    }
    
    Powershell
    Matching with ^RP\.[A-Za-z]*\.[A-Za-z]*[A-Za-z0-9] :
    _______________________________________________________________
    
    The name RP.Lizzie.Borden is o.k.?
    The name RP.Ma.Barker is o.k.?
    The name RP.Atilla.Hun is o.k.?
    The name RP.Ted.Bundy7 is o.k.?
    The name RP.Al.Capone2 is o.k.?
    The name RP.J.J is o.k.?
    The name RP.J.1 is o.k.?
    The name RP.J.2 is o.k.?
    
    
    
    Matching with ^RP\.[A-Za-z]+\.(?:(?=[a-zA-Z\d])[A-Za-z]*\d*) : 
    _______________________________________________________________
    
    The name RP.Lizzie.Borden is o.k.?
    The name RP.Ma.Barker is o.k.?
    The name RP.Atilla.Hun is o.k.?
    The name RP.Ted.Bundy7 is o.k.?
    The name RP.Al.Capone29 is o.k.?
    The name RP.J.J is o.k.?
    The name RP.J.1 is o.k.?
    The name RP.J.22 is o.k.?
    
    While your match was good enough for identification, you can see, that in a use case as above, where you need the fill matched string, it will be missing the full name as in the case of RP.Al.Capone29 and RP.J.22.
    Your script worked because it listed whole lines that had a match, while I'm using the actual match itself....
    • local_offer Tagged Items
    • NeallyNeally
    Was this post helpful? thumb_up thumb_down
  • please don't use 'echo' in powershell... it's write-output.

    OP confirmed it worked for their use case. ¯\_(ツ)_/¯

    And yes it's a lazy match, not a complete match.

    What works depends on what input data OP has.

    You could just add instances with 1 or 2 matches, many ways

    Powershell
    ^RP\.[A-Za-z]*\.[A-Za-z]*[A-Za-z0-9]{1,2}
    
    Was this post helpful? thumb_up thumb_down
  • Neally wrote:

    please don't use 'echo' in powershell... it's write-output.

    OP confirmed it worked for their use case. ¯\_(ツ)_/¯

    And yes it's a lazy match, not a complete match.

    What works depends on what input data OP has.

    You could just add instances with 1 or 2 matches, many ways

    Powershell
    ^RP\.[A-Za-z]*\.[A-Za-z]*[A-Za-z0-9]{1,2}
    

    Most important is, the op got what he needed.

    For the rest.... these patterns often depend on the purpose we need them for. I don't remember what 'rule' it was, that made me search for the 'complicated' version. Something was (probably even wrong understanding the initial rule) that convinced me, that the 'simple' version wouldn't do the job correctly.

    Otherwise, I'm using regex most of the time for text (log files) filtering and text editing (text replacements). Specially in the later, lazy matches are something one tries to avoid as far as possible. That is also the reason, that it pretty fast caught my attention, that the initial expression was a 'lazy' match.

    For the purpose of identifying false syntax in the name string, the lazy version was good enough.

    But it's still good to point out, that for some other purpose we would need a slightly less 'lazy' version.

    Spice (1) flagReport
    Was this post helpful? thumb_up thumb_down
  • Fellows, sorry about the long silence, I have been working more than 12 hours a day lately.   I need to go over some things with you geniuses later.  Thank you very much.  You have been a big, big help to me.. 

    Was this post helpful? thumb_up thumb_down
  • Fellows,

    I cannot find anything wrong with these two

     ^IO\.[A-Za-z]*\.[A-Za-z]*[A-Za-z0-9]

     ^IO\.[A-Za-z]+\.(?:(?=[a-zA-Z\d])[A-Za-z]*\d*)

     Can they also include this?  

    I would like it to match zero, one, or two   numbers at the end of the string.

    No more than two numbers at the end.

    Numbers only at the end, nowhere else

     I would like it to   also   match  these too

    RP.Attila.Hunn           (Zero Numbers)

    RP.Attila.Hunn1         (One Number)

    RP.Attila.Hunn12       (Two Number)  


     But not:

    RP.Attila.Hunn123    (Too many numbers

    RP.Attila.12Hunn       (Numbers are not at the end).

    RP.Attila.H1unn       (Number is not at the end).

     I have been trying with many different versions of  \d{2}$/

    But I haven’t  had any good success yet.


    Was this post helpful? thumb_up thumb_down
  • use                        \d{0,2}

    \d =  digit character

    {0,2} = at least 0 times and up to 2 times the character before this

    Spice (1) flagReport
    Was this post helpful? thumb_up thumb_down
  • Bojan, Thank you for your reply.

    I tried your suggestion: \d{0,2}

    I used this:   RP\.[A-Za-z]+\.(?:(?=[a-zA-Z\d])[A-Za-z]*\d{0,2})

    Then I tried it out here:  

    regex101: build, test, and debug regex 

    But when I try it out on PowerShell:

    $username = "RP.Lizzie.Borden123"
    $Test =  $username -Cmatch  '^RP\.[A-Za-z]+\.(?:(?=[a-zA-Z\d])[A-Za-z]*\d{0,2})'
    Write-Host “$ Test " $Test $username

    I got this result:
    $ Test  True RP.Lizzie.Borden123
    
    
    It seems to me that this:   RP.Lizzie.Borden123     Should return False 

    I don't want it to match.  

    So how can I change this?       ^RP\.[A-Za-z]+\.(?:(?=[a-zA-Z\d])[A-Za-z]*\d{0,2})

    Thanks in advance for all your help.

    Was this post helpful? thumb_up thumb_down
  • ManFromKansas wrote:

    -Cmatch

    Why are you using cmatch? rather than match, why does it need to be case-sensitive, yet you match either case? [A-Za-z]

    Your requirenents aren't multually exclusive, so I'd think an OR statement might be needed.

    Powershell
    clear
    $stuff = 
    @"
    RP.Attila.Hunn
    RP.Attila.Hunn1
    RP.Attila.Hunn12
    RP.Attila.Hunn123
    RP.Attila.12Hunn
    RP.Attila.H1unn
    RP.Lizzie.Borden
    RP.Ma.Barker
    RP.Atilla.Hun
    RP.Ted.Bundy7
    RP.Al.Capone29
    RP.J.J
    RP.J.1
    RP.J.22
    RP.Lizzie7.Borden
    RP.Lizzie.
    RP.Lizzie
    "@ -split "`r`n"
    
    foreach($line in $stuff){
        if($line -cmatch "^RP\.[A-Za-z]*\.\d{1,2}\b|^RP\.[A-Za-z]*\.[A-Za-z]+[0-9]{0,2}\b"){
            $Matches.Values
        }
    }
    

    Matches:

    Powershell
    RP.Attila.Hunn
    RP.Attila.Hunn1
    RP.Attila.Hunn12
    RP.Lizzie.Borden
    RP.Ma.Barker
    RP.Atilla.Hun
    RP.Ted.Bundy7
    RP.Al.Capone29
    RP.J.J
    RP.J.1
    RP.J.22
    

    IDK if that's what you need, and I'm sure Bojan has something smarter

    Was this post helpful? thumb_up thumb_down
  • ManFromKansas wrote:

    Bojan, Thank you for your reply.

    I tried your suggestion: \d{0,2}

    I used this:   RP\.[A-Za-z]+\.(?:(?=[a-zA-Z\d])[A-Za-z]*\d{0,2})

    Then I tried it out here:  

    regex101: build, test, and debug regex 

    But when I try it out on PowerShell:

    $username = "RP.Lizzie.Borden123"
    $Test =  $username -Cmatch  '^RP\.[A-Za-z]+\.(?:(?=[a-zA-Z\d])[A-Za-z]*\d{0,2})'
    Write-Host “$ Test " $Test $username
    I got this result:
    $ Test  True RP.Lizzie.Borden123
    
    
    It seems to me that this:   RP.Lizzie.Borden123     Should return False 

    I don't want it to match.  

    So how can I change this?       ^RP\.[A-Za-z]+\.(?:(?=[a-zA-Z\d])[A-Za-z]*\d{0,2})

    Thanks in advance for all your help.

    You forgot to add the $ characher at the end, so you would match only the strings that have a line end after the last 0-2 digits

    https://regex101.com/r/SlzM5j/1

    Was this post helpful? thumb_up thumb_down
  • Neally, Yes, you are right, I do not need -CMatch.  I will take it out and use -Match.   

    And thank you for showing me that better way to test many inputs in Powershell.  I know I will start using that right away.  

    Bojan, thank you for reminding me to use the $    Now it works.  I tried to use it earlier but I did not get it to work.  

    Many thanks to both of you.  You have really helped me.  I understand much of the code you showed me but I need to study the finer details so I will be able to make modifications in the future.  

    I work for a very large Government Agency as a lowly Active Directory Administrator.   Only one other person on my team uses PowerShell but not Regex.  .   But I am coming up with some scripts for Bulk requests that sometimes update/change scores or even hundreds of users.  So I am going to use this Regex with one of my scripts to show the boss.   

    Was this post helpful? thumb_up thumb_down
  • ManFromKansas wrote:

    I work for a very large Government Agency as a lowly Active Directory Administrator.   Only one other person on my team uses PowerShell but not Regex.  .   But I am coming up with some scripts for Bulk requests that sometimes update/change scores or even hundreds of users.  So I am going to use this Regex with one of my scripts to show the boss.   

    I'm an old-school DOS user and ignored the existence of Powershell for a long time. Than I said, that it would be good to learn a bit of it... who knows how long will the old DOS type command shell be available..... but it was a bit too much different to simply see&remember.... so I've put the plans to 'learn it' on the side. Probably I will never really 'learn it'. But thanks to google and tons of great Powershell code snippets, even someone like me, who has not learned Powershell, can put together some script, to automate a task, that needed repeating manual steps or even things that wouldn't be solvable without Powershell.

    Regular expressions are something completely different, because I'm using them in firewall management to filter logs, so they show only what I need to see, use them in text editors (also some pretty complex text manipulation) and I use it in tools like TotalCommander, whenever they support Regex. It's by far one of the most useful things I have learned for a long time. But I'm no expert for Regex... would say that I'm more an 'advanced beginner'. Luckily many of the tools that support Regex also have aids, that help you use it.

    Still I wonder, that there is so few non-programmers, who actually know and use Regex. How often has it helped me scrape some text output into a form, that was 'importable' to excel... Things that would be practically impossible without Regex.

    You got the idea, what it can be good for, so I can only encourage you to keep using it wherever you can. Together or without Powershell. It just as well works in Bash, Python,.... the list is really long.

    Was this post helpful? thumb_up thumb_down
  • Bojan, thank you for your insights.  I took a DOS class about 30 years ago at the local Junior College.   And I am like you with PowerShell,  I understand enough to put together some scripts.   I studied Don Jones' book, "Learn Powershell In A Month Of Lunches"  and some others.    And I have come very close to ordering

    Mastering Regular Expressions Third Edition

    by Jeffrey E. F. Friedl 

    Was this post helpful? thumb_up thumb_down
  • The following will work for your provided test cases:

    Powershell
    '^RP\.[a-z]+\.[a-z]*\.?[0-9]{0,2}(?<!\.)$'
    

    Here is how to prove it:

    Powershell
    $testcases = 'RP.Lizzie.Borden','RP.Ma.Barker','RP.Atilla.Hun',
                 'RP.Ted.Bundy7','RP.Al.Capone29','RP.J.J',
                 'RP.J.1','RP.J.22','RP.Lizzie7.Borden',
                 'RP.Lizzie.','RP.Lizzie'
    $regex = '^RP\.[a-z]+\.[a-z]*\.?[0-9]{0,2}(?<!\.)$'
    switch -regex ($testcases) {
        $regex { "$_ : True" }
        default { "$_ : False" }
    }
    

    d

    Was this post helpful? thumb_up thumb_down

Read these next...