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
    what did you mean by any string? does that means regex .+ will do? and btw, \d and _ is already included inside \w Commented Aug 1, 2013 at 17:39
  • You don't really describe very well what exactly you're trying to match inside the brackets. From what I can infer from you're code, you're looking for any string that just has alphanumeric characters and the underscore. If that's the case, you could just use \[[\d\w]*\] or \[[a-zA-Z_0-9]*\].... Oh, I see a colon and dash in there too. Well, in that case you can do \[[\w\d:-]*\]. Commented Aug 1, 2013 at 18:11
  • Angga got it on point. By any string I meant "Any Possible characters you can place in between 2 brackets". The .+ handles that exactly. Thank you, post your solution so I can accept it as an answer Commented Aug 1, 2013 at 18:31
  • @Cole9350 It would be better to update the question with this information. Commented May 29, 2017 at 17:41
  • @PeterMortensen Please do not use your automated software to edit my questions / answers. The edits are not useful and do not make sense. Wikipedia is not a valid source to site for the change you suggested. Commented Jun 21, 2017 at 19:05

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.