-1

I want to extract the texts from between the square brackets and i am using [(.*?)] but it is not working completely.

Apr 17 13:01:34 10.11.26.83 2012-04-17 00:30:52.222 -0700 ABCD XYZ ALER SOME_TEXT 99.99.182.1 49923 99.99.83.74 80 blocked-policy GLOBAL DENY NONE [type="SOME TEXT" pattern="some-random-pattern" token="/function()"] GET 99.99.83.74/index.html/function() HTTP "-" "Wget/1.12 (linux-gnu)" 99.99.182.1 49923 "-" "-"

I want to extract the bold texts. What is the regular expression to do this?

rprakash
  • 500
  • 5
  • 10

2 Answers2

1

@rprakash:

Anatomy of the pattern

<!--
\[(.*?)\]

Match the character “[” literally «\[»  
Match the regular expression below and capture its match into backreference number 1 «(.*?)»  
   Match any single character that is not a line break character «.*?»  
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»  
Match the character “]” literally «\]»  
-->

While using REGEX it is always safe to use negated character class instead of dot operator.

It might be \[[^\]]+\] as Bohemian said, or may be safer to use \[(?#if grouping required)([^\]\[]+)\].

For making better footholds in this subject visit here.

Cylian
  • 10,970
  • 4
  • 42
  • 55
0

Try this:

\[[^\]]+\]

[^\]] is a character class for "any character other than a ]"

Bohemian
  • 412,405
  • 93
  • 575
  • 722