Hi Experts,

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


- 85020-VWUPXXX01

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

- [85020-VWUP]XXX01
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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? may help you figure things out a little bit.
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.
cmatchettAuthor Commented:
So the string could be   $Name = 'SC-EU-T-COL1'  OR $Name = '85020-VWUPCOL1'

If match will do then that is amazing
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

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'
cmatchettAuthor Commented:
if i always had SC-EU-T or SC-US-T, would match work?
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
cmatchettAuthor Commented:
in another variable i will always have $ENV = 'SC-EU-T', for example

So would $name -match $env work?
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') {
} Else {
	Write-Error "Unexpected name format found: $($Name)"

Open in new window

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.
cmatchettAuthor Commented:
a CSV file that contains a number of properties, sample below


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

Example data from one environment would be



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
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. 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

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

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

can you please explain more what this does

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

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
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
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
cmatchettAuthor Commented:
good suggestions from both members.  -match was good enough
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

From novice to tech pro — start learning today.