Astaar
Astaar

Reputation: 6568

Applying Regex with enclosing string

I'm new to Regex and I'm trying to extract a path from a string found inside a file. But there are many strings that could match the regex I managed to come up with so I came to the conclusion I need to apply the string only if it's enclosed between static strings.

The file looks like this:

14:58:15 [LC] AKF = 0-65535
14:58:15 [THR] CPU 02 : Engine (AFK)
14:58:15 [QA] Result Path : [/some/linux/path/Results/Test_2013_09_12_14_58_15]
14:58:15 [THR] Starting Listener
14:58:15 [THR] Starting Listener

What I'm not sure is if with a regex I could create a pattern that includes the enclosing strings ("[QA] Result Path : " and "]") or if I need to code that logic?

I'm using OpenJDK 1.7.

Upvotes: 0

Views: 93

Answers (2)

djna
djna

Reputation: 55907

You seem to think that you can use only the pattern matching characters in your regex. You can include literals:

  abc.*def

Will search for "abc" followed by any number of any characters followed by "def". If the characters you want to search for are "special", for example a . or a ] then you will need to escape them with a .

Upvotes: 1

Display Name
Display Name

Reputation: 8128

\[(.*?)\].*?\[(.*?)\]$

It should capture contents of both brackets.

$ is "end of string", you can omit it if it's not important.

Upvotes: 0

Related Questions