17

No matter how well I feel like I know regular expressions, they always seem to beat me.

I am looking for a universal pattern that will match any string. The only way I could figure out how to handle all these different naming conventions, was make a bunch of different regex patterns, and now I'm not even sure if all the data is getting picked up so I would have to manually cross-check it.

I am just trying to pick up anything that could possibly be within two brackets [ ] :

elseif($line -match "\[\w*\d*\]") {         
    $pars = $matches[0]
}
elseif($line -match "\[\d*\w*\]") {
    $pars = $matches[0]
}
elseif($line -match "\[\w*\d*_\w*\]") {
    $pars = $matches[0]
}
elseif($line -match "\[\w*\d*_*\w*-*\w*:*\w*\]") {
    $pars = $matches[0]
}            
elseif($line -match "\[\w*_*\w*_*\w*_*\w*_*\w*_*\w*-*\w*\]") {
    $pars = $matches[0]
}

The way I am doing it does not generate errors, but I am not sure it handles all the situations I could potentially come across. Checking manually is almost impossible with this much data.

Also, if anyone knows of a great utility for generating regex patterns it would be much appreciated. I have only been able to find regex testers which isn't very useful to me, and there is little help online for regular expressions with powershell.

5

4 Answers 4

21
$a = [regex]"\[(.*)\]"
$b = $a.Match("sdfqsfsf[fghfdghdfhg]dgsdfg") 
$b.Captures[0].value
Sign up to request clarification or add additional context in comments.

5 Comments

Perfect, This works just as well as .+ , which leaves me to figure out the difference between greedy and ungreedy. Thank you very much.
Sorry I don't understand your question ?
what happens if a string contains more than one ], ex "asdfasfd[asdf]sfafe]asdfs"? How to get "asdf" within [..]?
Use $a.Matches() instead of Match() to return multiple results - Match() seems to find one and then stop
@David.Chu.ca, $a = [regex]"\[([^\[\]]*)\]" will stop it from matching against the 2nd ]
7

Match everything that isn't a bracket. Create a character class that contains anything but the bracket characters:

$line -match "\[[^\[\]]+\]"

Comments

2

Focusing to

"I am just trying to pick up anything that could possibly be within two brackets [ ]"

I think \[.*\] is what you are looking for.

Explanation
Sice [ and ] have special purposes, so you need to use \ before those characters.
.(dot) stands for any character and
* stands for any number of repetition of the previous charector.
Here, the previous character is ., so .* stands for any general string.

Comments

0

Matching anything between two brackets:

$text='[a[b]c] and [d text]'
$p = [regex]'\[[^][]*]'
$p.Matches($text) | % {Write-Host $_.value}

See regex proof. Results:

[b]
[d text]

EXPLANATION

--------------------------------------------------------------------------------
  \[                       '['
--------------------------------------------------------------------------------
  [^][]*                   any character except: ']', '[' (0 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  ]                        ']'

If the brackets need to be paired and can be nested:

\[(?>[^][]+|(?<g>)\[|(?<-g>)])*]

See regex proof. EXPLANATION

--------------------------------------------------------------------------------
  \[                       '['
--------------------------------------------------------------------------------
  (?>                      match (and do not backtrack afterwards) (0
                           or more times (matching the most amount
                           possible)):
--------------------------------------------------------------------------------
    [^][]+                   any character except: ']', '[' (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
  (?<g>)\[                '[' (empty match added to "g" group stack)
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
  (?<-g>)]                ']' (empty match removed from "g" group stack)
--------------------------------------------------------------------------------
  )*                       end of look-ahead
--------------------------------------------------------------------------------
  ]                        ']'

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.