tags:

views:

77

answers:

4

cud any body tell me how this expression works

output = "#{output.gsub(/grep .*$/,'')}"

before that opearation value of ouptput is

"df -h | grep /mnt/nand\r\n/dev/mtdblock4  248.5M    130.7M    117.8M  53% /mnt/nand\r\n"

but after opeartion it comes

"df -h | \n/dev/mtdblock4          248.5M 248.5M    130.7M    117.8M  53% /mnt/nand\r\n "

plzz help me

+2  A: 

Your expression is equivalent to:

output.gsub!(/grep .*$/,'')

which is much easier to read.

The . in the regular expression matches all characters except newline by default. So, in the string provided, it matches "grep /mnt/nand", and will substitute a blank string for that. The result is the provided string, without the matched substring.

Here is a simpler example:

"hello\n\n\nworld".gsub(/hello.*$/,'') => "\n\n\nworld"

In both your provided regex, and the example above, the $ is not necessary. It is used as an anchor to match the end of a line, but since the pattern immediately before it (.*) matches everything up to a newline, it is redundant (but does not cause harm).

jason.rickman
THANKS jasonbut cud u plzz expain with some other example
amit singh tomar
and one more thing jason why this *$ is used plus /n is also at the end of line
amit singh tomar
@amit : I've updated the answer to include another example and an explanation of $ (and why it's not needed here). Think of the .* going together and the $ as a separate symbol.
jason.rickman
thanku very much friend
amit singh tomar
nd one thing more will it these \n\n\\n in o/pi think it will o/p as world
amit singh tomar
The newlines "\n\n\n" will remain, because they are not matched by the regex. The (.*) matches up to, but not including, a newline character. Notice in your original string, that the \n remains. Everything up to and including the \r is stripped (\r is not considered newline)
jason.rickman
A: 

Since gsub returns a string, your first line is exactly the same as

output = output.gsub(/grep .*$/, '')

which takes the string and removes any occurance of the regexp pattern

/grep .*$/

i.e. all parts of the string that start with 'grep ' until the end of the string or a line break.

Michael Ulm
thanks ULM line break means this /r or /n thats the line break??nd why *$ is used
amit singh tomar
The exact definition of line break is implementation and platform dependent, but \n is definitely a line break. Regular expressions usually stop when they encounter the \n, unless you explicitely tell them otherwise..* means 'match any character zero or more times'. So anything after the 'grep ' is matched.$ in the RegExp is a special character representing the end of the line.
Michael Ulm
A: 

There's a good regexp tester/reference here. This one matches the word "grep", then a space, then any number of characters until the next line-break (\r or \n). "." by itself means any character, and ".*" together means any number of them, as many as possible. "$" means the end of a line.

glenn mcdonald
glenn thanks first of allcud u plzz expain this .* that i m nt able to unstand yetplzz i m waiting
amit singh tomar
A period by itself means any character, and an asterisk after anything means as many of those as possible. So put together ".*" gets everything on the rest of the line after the "grep ".
glenn mcdonald
A: 

For the '$', see here http://www.regular-expressions.info/reference.html

".*$" means "take every character from the end of the string" ; but the parser will interpret the "\n" as the end of a line, so it stops here.

phtrivier