2

I'm new posting to this site, but use it extensively.

I'm using the following regex pattern:

(\w{2})_(\w{3}) (\w{2,4}) ([\w ]+) - ([\w ]+)

Works perfectly on any of the following Strings when trying to match:

Rg_Svr GB This is a Test - Permission
Rg_Svc TW ThisIsATest - Permission Also

It also works on the following, but I don't want it to:

Rg_Svc TW ThisIsATest - Permission Also - AndMoreText
Rg_Svc TW ThisIsATest - Permission Also - And More Text

Basically, I don't want Powershell to have a successful match if there is anything after the 5th Capturing Group.

Results from match are:

1.  [0-2]   `Rg`
2.  [3-6]   `Svc`
3.  [7-9]   `TW`
4.  [10-21] `ThisIsATest`
5.  [24-40] `Permission Also `

Thanks in advance!

1 Answer 1

2

The problem is that your expression will match the first section of the text you do not want to match. You can add the ^ and $ anchors to expect an exact match, something like so: ^(\w{2})_(\w{3}) (\w{2,4}) ([\w ]+) - ([\w ]+)$. An example can be viewed here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.