tags:

views:

461

answers:

7

what does the regular expression $$ evaluate to?

A: 

That can depend on the exact type of REs in use. In the commonly-used POSIX basic REs, $ means, basically, "end of line". So this would match a line followed by an empty line.

unwind
@unwind: "this would match a line followed by an empty line" -- It wouldn't. $ is zero-width. ;-)
Tomalak
Okay ... I tested it, but maybe not in the correct environment (I used EditPad Pro in Windows, since I already had it running).
unwind
In EPP's search box, matches are always in Multiline mode, so $ matches the end of a line or the end of the document. But it doesn't match any characters.
Alan Moore
+2  A: 

It just depends on the underlying regex engine. In vim it would match a dollar sign at the end of line, for example. I'd guess that Posix would require the engine to match the same as '$' since that literal has 0 length as Tomalak pointed out.

soulmerge
I didn't know that vim would do this... +1
Tomalak
Only the $ at the end of the regex behaves posixly corerct in vim, every other $ matches the dollar sign.
soulmerge
+1  A: 

It depends on the underlying regex engine. But in the .Net framework System.Text.RegularExpressions.Regex class, $ within regular expressions specifies that the match must occur at the end of the string, before \n at the end of the string, or at the end of the line, so $$ will match everything!

($$ has another meaning in replacement patterns, which substitutes a single "$" literal.)

M. Jahedbozorgan
"$$" will match everything, in terms of "every character in the string"? Are you sure?
Tomalak
"$$" will match everything, in terms of "every string".//The following will return true for any given textprivate static bool IsMatch(string text){ return new System.Text.RegularExpressions.Regex("$$").IsMatch(text);}
M. Jahedbozorgan
Hm... When you look at it that way, it's true. You made it sound like it was a special property of "$$", but it is equally true for "^" or "$" or ".*". The key question remains unanswered - what does it *match*?
Tomalak
Yes, "^" or "$" or ".*" also match everything! because they are actually equal in functionality to "" (zero-width string) which matches everything.
M. Jahedbozorgan
A: 

Normally is a matcher for end-of-line but this depend on which language are you using. Without this specific information is difficult to answer.

BTW, in csh means the process number. It is not a regular expression.

Luis

Luixv
Didn't know that csh was a regex engine ...
soulmerge
+2  A: 

That construct is unlikely to occur except in languages that allow variable interpolation in the regex. In that case it most likely will match the process id of the current process (since $$ is generally a variable that holds the process id).

Chas. Owens
+3  A: 

in Perl, embedding $$ into a regular expression will result in the PID of the perl process being inserted

# Suppose perl has a PID of 5432
my $str1 = "some";
my $str2 = "5432";

print "1 match: $str1" if $str1 =~ /^$$/;
print "2 match: $str2" if $str2 =~ /^$$/;

Output:

2 match: 5432

Inserting a single $ will match the end of line.

dsm
+2  A: 

It depends on the programming language and regexp engine.

  • In Perl, /$$/ matches the PID (process id) of the current process (see more in dsm's answer).
  • In Ruby, both /$$/ and /$/ match the empty string before the first newline, or the end of the string if there are no newlines in the string.
  • In Python, both re.search('$$', s) and re.search('$', s) match the empty string before the last newline, or the end of the string if there are no newlines in the string.
  • (Other languages or regexp engines may behave differently.)

Please note that the flags (usually the s and the m flags) associated with the regexp affect what $ matches. The items above use the default flags.

pts