RegEX Help

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
shortaug
Forum Newbie
Posts: 13
Joined: Wed May 13, 2009 6:11 pm

RegEX Help

Post by shortaug »

I have an array of lines.

ex:
02/05/2009,12:, [sbh\dekk44] Test [Who Boss] weeekddseee:
4/3/2003,12:, [ahell] Test [Who Boss] weeekddseee:

I need to draw out the string within the [ ] as long as they:
  • have sbh\ or no sbh\
    have no white space within the [ ]
    can be any length greater than 1
    can contain any number of numbers or letters
In the example above, only the sbh\dekk44 and ahell are what I want.

Currently, I have:
preg_match("#\[(\w+)\]#i", $line, $matches)

It correctly matches ahell, but I cannot figure out how to match the sbh\ or it's lack thereof.

Any help?
shortaug
Forum Newbie
Posts: 13
Joined: Wed May 13, 2009 6:11 pm

Re: RegEX Help

Post by shortaug »

heh, figured it out just a few seconds later:

preg_match("#\[((sbh.)?\w+)\]#i", $line, $matches)
Post Reply