REGEX | POWERSHELL

Hi Experts,

I'm feeling perplexed with trying to understand regex :(

- SC-EU-T-XXX01
- SC-US-T-XXX01
- SC-EU-S-XXX01
- VW-US-S-XXX01

- VWUPXXX01
- 85020-VWUPXXX01
- SCEPXXX01

From the above, i always want to get the text in square brackets

- [SC-EU-T]-XXX01
- [SC-US-T]-XXX01
- [SC-EU-S]-XXX01
- [VW-US-S]-XXX01

- [VWUP]XXX01
- [85020-VWUP]XXX01
- [SCEP]XXX01
cmatchettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Adam BrownSr Solutions ArchitectCommented:
I'm feeling perplexed with trying to understand regex :(

You and everyone else that starts out learning it. It's not really intuitive.

At any rate, can you please clarify what you're trying to accomplish? Are you trying to search data by looking only at the section before the XXX and return only a specific portion?

https://regexr.com/ may help you figure things out a little bit.
0
oBdACommented:
Might not be enough information.
Is XXX01 a constant string, or are the XXX placeholders? If they're placeholders: for what, and is it always three characters?
Is the 01 a constant and/or can the 01 be another number?
If it's a constant, it's rather simple:
$a = 'SC-EU-T-XXX01',  'SC-US-T-XXX01', 'SC-EU-S-XXX01', 'VW-US-S-XXX01', 'VWUPXXX01', '85020-VWUPXXX01', 'SCEPXXX01'
$a -replace '-?XXX01'

Open in new window

If it's not a constant, please elaborate.
0
cmatchettAuthor Commented:
So the string could be   $Name = 'SC-EU-T-COL1'  OR $Name = '85020-VWUPCOL1'

If match will do then that is amazing
0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

cmatchettAuthor Commented:
So the string could be   $Name = 'SC-EU-T-COL01'  OR $Name = '85020-VWUPCOL01'
So the string could be   $Name = 'SC-EU-T-COL02'  OR $Name = '85020-VWUPCOL02'
0
cmatchettAuthor Commented:
if i always had SC-EU-T or SC-US-T, would match work?
0
cmatchettAuthor Commented:
So the string could be   $Name = 'SC-EU-T-XXX01'  OR $Name = '85020-VWUPXXX01'
So the string could be   $Name = 'SC-EU-T-XXX02  OR $Name = '85020-VWUPXXX02'

The xxx can a number of combinations
0
cmatchettAuthor Commented:
in another variable i will always have $ENV = 'SC-EU-T', for example

So would $name -match $env work?
0
oBdACommented:
If you have a variable already containing the string you need, why match it against the name instead of using this variable directly?

Please describe exactly (not by a few select examples) how the last 5 characters are defined.
Is it always the last 5 characters that need to be removed (aside from the hyphen that might or might not be there)?
Or do you only want COL and JOL, but not XOL?
$Name = '85020-VWUPjOL02' # 'SC-EU-T-COL01'
If ($Name -match '(?<Keep>.*?)-?\DOL\d\d\Z') {
	$Matches['Keep']
} Else {
	Write-Error "Unexpected name format found: $($Name)"
}

Open in new window

0
Adam BrownSr Solutions ArchitectCommented:
What are you trying to do, exactly? Powershell it useful for this, but we need to know what your end goal is to create an effective match rule.
0
cmatchettAuthor Commented:
a CSV file that contains a number of properties, sample below

EnvName,NamingConvention
US-TEST, SC-US-T
EU-TEST, SC-EU-T

i am attempting to work out what environment a virtual machine belongs to based on its name

Example data from one environment would be

SC-EU-T-XXX01
SC-EU-T-XXXX01

SC-US-T-XXX01
SC-US-T-XXXX01

xxx and xxxx can be different combinations and the number at the end can be anything up to 99

so trying to get a simpe solution, i thought i could loop through the CSV and have a solution like
 (if $EnvName -Match $Name)
{
    $Environment = $EnvName
}
0
Adam BrownSr Solutions ArchitectCommented:
You would want to use -like instead of -match for your case. -match doesn't allow wildcards and would require exact matching of strings. -like, however, is a comparison system that allows wildcards. A * counts allows anything before or after a specific string.  https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/string-wildcard-syntax covers the syntax for it a little more in depth. You can use Regex with -like, I think.

It would be like this:
$name = "sc-eu-t-*"
if ($envname -like $name)
{
   $environment = $envname
}

Open in new window

0
oBdACommented:
That would be the NamingConvention column you need to compare to $Name, not EnvName
$Csv = Import-Csv Whatever.csv
$Environment = $Csv | Where-Object {$Name -match "\A$($_.NamingConvention)"} | Select-Object -ExpandProperty EnvName

Open in new window

0
aikimarkCommented:
you should be able to use -match "(.+)(?:-.+|.{5})(?:\r\n|$)"
0
cmatchettAuthor Commented:
using match by itself was good enough

can you please explain more what this does

-match "(.+)(?:-.+|.{5})(?:\r\n|$)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aikimarkCommented:
can you please explain
(.+) capture 1 or more characters
(?:-.+|.{5}) followed by by either

hyphen character followed by 1 or more characters followed by a space character
or
space character followed by 5 characters
Do not capture this trailing data

(?:\r\n|$) terminated by either a CrLf combo or the end of the string.  Do not capture this terminating data
0
cmatchettAuthor Commented:
good suggestions from both members.  -match was good enough
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.