Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regex Match If not preceded by keyword [closed]

Tags:

java

regex

This is a regex specific question..

I need a regex that can find a certain keyword.. Example ABCDE But it should not match if certain characters precede this keyword.. Example ]]

So... In a line like this, it should only match the bold part..

[[TestChannel]] ABCDE: this is a test ABCDE

EDIT: I have been trying things out here.. http://gskinner.com/RegExr/

So far what i have tried..

(!]])(ABCDE)
((!]])ABCDE)
(!]])!(ABCDE)
((!]])|ABCDE)

Solution: With help from the link Pshemo commented -> http://www.regular-expressions.info/lookaround.html#lookbehind

(?<!]])ABCDE
like image 510
Prathamesh Gharat Avatar asked Nov 10 '12 09:11

Prathamesh Gharat


People also ask

What does \b mean in regex?

The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a “word boundary”. This match is zero-length. There are three different positions that qualify as word boundaries: Before the first character in the string, if the first character is a word character.

What does \+ mean in regex?

Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, inclusive the newline character itself. Example: "a\+" matches "a+" and not a series of one or "a"s. ^ the caret is the anchor for the start of the string, or the negation symbol.

What does \\ mean in Java regex?

String regex = "\\."; Notice that the regular expression String contains two backslashes after each other, and then a . . The reason is, that first the Java compiler interprets the two \\ characters as an escaped Java String character. After the Java compiler is done, only one \ is left, as \\ means the character \ .

What is \r and \n in regex?

\n. Matches a newline character. \r. Matches a carriage return character.


1 Answers

(?<!]])ABCDE

Thanks Pshemo for the links.

like image 137
Prathamesh Gharat Avatar answered Sep 28 '22 08:09

Prathamesh Gharat