Can't get the character set of [^]] to match "]" in regex (([^[])(-?\w)/)|(/(-?\w)([^]])).

NevSoFly
NevSoFly used Ask the Experts™
on
I'm trying to create a regex that only matches when:
An alphanumeric character that is not preceded by "[" but is followed by "/"
and a "/" & an alphanumeric character when there isn't a "]" in front of it.

for example:
1[ts/tty]/a..[a/bravo] ntf/dt; c/[antenna/board] --->  should match /a, /d and c/

So far I have (([^[])(-?\w)/) ---> this matches /d and c/ but not /a.  I have tried (([^[])(-?\w)/)|(/(-?\w)([^]])) but can't get the character set of [^]] to match "]".
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2015

Commented:
(?<!\[)[a-zA-Z0-9](?=/)|(?<!\])/[a-zA-Z0-9]

Open in new window


** edit

Does "preceded" mean "immediately preceded," or just "preceded"? Also, what programming language or text editor are you using?

Author

Commented:
Thanks for responding.  I mean immediately preceded and vb.net.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Also, why would "f/" not satisfy the criteria? In other words, why does "/d" match, but "f/" does not?
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Most Valuable Expert 2011
Top Expert 2015

Commented:
And I do not understand why "/a" matches. According to your criteria:

"/" & an alphanumeric character when there isn't a "]" in front of it.

But there most assuredly is a ] in front of the "/a".

Author

Commented:
That was a typo. f/ should also match.  I'm sorry I was writing the example on the fly and missed my mistake.

Author

Commented:
I'm doing a terrible job at explaining what I want. Sorry.  A / and alphanumeric character or an alphanumeric character and / should always match unless they are inside a set of [].
Most Valuable Expert 2011
Top Expert 2015
Commented:
That makes a bit more sense  = )

Try:

(?<!\[[^]]+)(/[a-zA-Z0-9]|[a-zA-Z0-9]/)(?![^[]+\])

Open in new window

Most Valuable Expert 2011
Top Expert 2015

Commented:
I made an edit to the above just now. Forgot one small character!

Author

Commented:
I am using regexr.com to test the patterns out but your suggestion didn't match anything.
Most Valuable Expert 2011
Top Expert 2015
Commented:
Try at http://regexhero.net/tester/

regexr.com isn't a .NET-based regex engine, and the constructs I used above only work in .NET (and one or two other regex engines).

Author

Commented:
Thanks for your help and suggestion.  I will start using regexhero from now on.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial