Tutorial :What does the regular expression $$ mean?



Question:

what does the regular expression $$ evaluate to?


Solution:1

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.


Solution:2

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.


Solution:3

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).


Solution:4

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.


Solution:5

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.)


Solution:6

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »