12

In a recent interview I was asked to decipher this regex

^\^[^^]

Can you please help me with it. Also please provide some links where I can learn regex for interviews.

7
  • 2
    It would be much clearer if you posted the actual regular expression, not the Java string with its painful and sometimes deceitful bbaacckkssllllaasshheesses.
    – tchrist
    Nov 3, 2010 at 12:15
  • 1
    @tchrist - why? he said that this character sequence was given to him in an interview, a Java related interview, I guess, looking at the tag. (BTW - the regex tag was added by someone else, originally it was tagged Java only) Nov 3, 2010 at 12:55
  • 1
    @Andreas_D, I hate to admit how many times I’ve screwed up a Java regex because of the backslash problem. Now I read them from a props file or as a command-line argument to (try to) avoid the problem. Even so, there’s a big difference between a Java literal used as a regex and an actual regex. It’s like fighting with the shell, but worse.
    – tchrist
    Nov 3, 2010 at 13:09
  • 2
    regexes do not need sequences of two backslashes to escape regex metacharacters, they need one backslash. The second backslash is only needed when encoding the regex as a Java string constant. The question asks to decipher the regex, not the regex as encoded in a Java string, and there are no surrounding quotes. I have therefore reverted the regex to that originally posted.
    – JeremyP
    Nov 3, 2010 at 13:44
  • 4
    Another interviewer who thought he was really smart. Jeez. Nov 3, 2010 at 14:27

4 Answers 4

30

It matches strings that begin with ^ followed by any character other than ^.

So it would match:

^foo
^b

but not

foo
^^b

Explanation:

Caret (^) is a regex meta character with two different meanings:

Outside the character class(1st use in your regex) it works as start anchor.

Inside the character class it acts like negator if used as the first character of the character class(3rd use in your regex).

Preceding a regex with \ escapes it (makes it non-special). The 2nd use of ^ in your regex is escaped and it matches a literal ^ in the string.

Inside a character class a ^ which is not the first character of the character class is treated literally. So the 4th use in your regex is a literal ^.

Some more examples to make it clear:

  • ^a         : Matches string beginning with a
  • ^ab       : Matches string beginning with a followed by b
  • [a]       : Matches a string which has an a
  • [^a]     : Matches a string which does not have an a
  • ^a[^a] : Matches a string beginning with an a followed by any character other than a.
8
  • 1
    That's true if you assume this regex is inside quotation marks. Otherwise, it would be a literal '\'. Nov 3, 2010 at 11:46
  • 2
    @Matt: I assumed that because of the java tag.
    – Matt K
    Nov 3, 2010 at 12:05
  • 3
    @Matt, I think it's a safe assumption given the fact that if the literal backslash was meant, the regex never would have matched any string.
    – Bart Kiers
    Nov 3, 2010 at 12:08
  • 1
    Since the regular expression is only matching 2 characters, I think the matching example would be better as it matches "^b" in "^bar" and "^f" in "^foo".
    – jwernerny
    Nov 3, 2010 at 13:28
  • I really ought to vote down since "Preceding a regex with \\ escapes it" is false. In a regex only one backslash is used to escape a character. I'm not voting down because you have assumed the regex is in a Java constant string, in which case regex escapes have to be string escaped.
    – JeremyP
    Nov 3, 2010 at 13:34
3

I'm testing this regex here however it does not seem to be valid.
The first ^ denotes the start of the line.
The first \ escapes the following \.
Thus the second "^" is not escaped Finally the first caret inside the square brackets [^ acts as the negation and second one ^] is not escaped as a result is not valid.

IMHO the correct regexp should be ^\^[^\^]
Guys, kindly confirm. Many thanks

2
  • 2
    I think there's an implicit assumption that this is inside quotation marks, so the two backslashes are necessary to escape the second caret. Nov 3, 2010 at 12:36
  • Thanks for that, just making sure
    – Philar
    Nov 3, 2010 at 12:45
2

Match beginning of line or string followed by a literal \ followed by the beginning of the line or string followed by any character that is not a space, return or new line character

8
  • Here is a popular site to learn regex: regular-expressions.info/tutorial.html
    – Mike Cheel
    Nov 3, 2010 at 11:45
  • That is wrong: you cannot have two beginning-of-string/line markers via ^ unless you are in (?m) mode. The most obvious answer is that it’s matching lines that start with a circumflex followed by any non-circumflex character, but this is hard to say because of the stooopid Java slackbashes.
    – tchrist
    Nov 3, 2010 at 12:40
  • I didn't say it was correct or would match anything. I just said what it says.
    – Mike Cheel
    Nov 3, 2010 at 13:12
  • @Mike Cheel: Most regex engines have four distinct rules for understanding circumflexes outside of (?m), and all are positionally dependent. Under customary rules and (?-m), outside of a bracket character class, only an initial ^ in the pattern counts as a metacharacter; subsequent ones do not. Therefore it cannot mean beginning of line more than once in the same pattern; it becomes a literal. Pretty nasty business, to be honest.
    – tchrist
    Nov 3, 2010 at 13:21
  • @tchrist I understand that. As I said I was just saying what the regex says. Throw it into an engine that interprets regexes (I used expresso) and you can see what I said it says is true.
    – Mike Cheel
    Nov 3, 2010 at 13:31
2

The first ^ is the beginning of line.

The second one is a literal character of ^ (\ is to escape the other usual meaning of ^)

The third one is to say

a class of characters which does not include the character ^

Some example to show using Ruby:

ruby-1.9.2-p0 > "hello" =~ /^h/    # it found a match at position 0
 => 0 

ruby-1.9.2-p0 > "hello" =~ /^e/    # nil means can't find it
 => nil 

ruby-1.9.2-p0 > "he^llo" =~ /\^/   # found at position 2
 => 2 

ruby-1.9.2-p0 > "he^llo"[/[^^]*/]  # anything repeatedly but not including the ^ character
 => "he" 
3
  • @動靜能量 - you ignored "the second one" in your examples - it will only match if the Strings start with a ^. And this ^ is part of the match ("^hel^lo" -> "^hel") Nov 3, 2010 at 11:55
  • the third case in the example is to show the matching of the literal ^... is that what you mean? Nov 3, 2010 at 11:59
  • @動靜能量 - My mistake, on first reading I thought those example would illustrate the behaviour of the original pattern. (BTW - wasn't who downvoted the answer) Nov 3, 2010 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.