regex matches numbers, but not letters

I have a string that looks like this:

[if-abc] 12345 [if-def] 67890 [/if][/if]

I have the following regex:


This matches the inner brackets just like I want it to. However, when I replace the 67890 with text (ie. abcdef), it doesn't match it.

[if-abc] 12345 [if-def] abcdef [/if][/if]

I want to be able to match ANY characters, including line breaks, except for another opening bracket [if-.


This part doesn't work like you think it does:


This will match a single character that is neither of [, i or f. Regardless of the combination. You can mimic the desired behavior using a negative lookahead though:


I've also included closing tags in the lookahead, as this avoid the ungreedy repetition (which is usually worse performance-wise). Plus, I've changed the delimiters, so that you don't have to escape the slash in the pattern.

So this is the interesting part ((?:(?!\[/?if).)*) explained:

(         # capture the contents of the tag-pair
  (?:     # start a non-capturing group (the ?: are just a performance
          # optimization). this group represents a single "allowed" character
    (?!   # negative lookahead - makes sure that the next character does not mark
          # the start of either [if or [/if (the negative lookahead will cause
          # the entire pattern to fail if its contents match)
          # match [if or [/if
    )     # end of lookahead
    .     # consume/match any single character
  )*      # end of group - repeat 0 or more times
)         # end of capturing group

Need Your Help

Java multi-thread programming - atomic operation

java multithreading concurrent-programming

I am on my way learning multi-thread programming with Java. Here is a confusion I got.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.