# Match until new line

**URL:** <https://www.ruby-forum.com/t/match-until-new-line/84768>\
**Category:** Ruby\
**Created:** [February 7, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/match-until-new-line/84768 "2007-02-07T16:06:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon1m0us](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@anon1m0us](https://www.ruby-forum.com/u/anon1m0us)\
**Post date:** [February 7, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/match-until-new-line/84768/1 "2007-02-07T16:06:53Z")

</div>

I am running a server utility that allows me to view server shares and  
permissions.

The command I am using is:  
SHARES=Array.new  
SHARES=`srvcheck \\testserver.split("\n")

This will change to be a variable.

the output is:

\testserver1\test  
domain\user1 Full Control  
Domain\Admin Full Control  
Domain\chuser Read  
Everyone Read  
domain\ysergeb Read

\testserver2\BACKUPTEST  
Everyone Change  
BUILTIN\Administrators Read

\testserver31\DEVQA$  
Everyone Full Control

\testsercver4\trymeout$  
BUILTIN\Administrators Full Control

I need to match to a server and a specified share and print out all  
the permissions just for that share. The .split("\n") spliuts it on  
the newline, but how can I get all the information in between the new  
lines.  
I need \testserver2 with all it’s permissions and ignore the rest.

---

<div class="post-metadata">

**Author:** ![anon1m0us](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@anon1m0us](https://www.ruby-forum.com/u/anon1m0us)\
**Post date:** [February 7, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/match-until-new-line/84768/2 "2007-02-07T16:42:58Z")

</div>

On Thu, Feb 08, 2007 at 12:05:06AM +0900, anon1m0us wrote:

> ```
> Everyone Change
> 
> ```
> 
> I need to match to a server and a specified share and print out all  
> the permissions just for that share. The .split("\n") spliuts it on  
> the newline, but how can I get all the information in between the new  
> lines.

.split("\n\n") will divide the output into groups split by a blank line.  
Then you can match on the start of the string to find the one you want.

#sharelist = `srvcheck \\\\testserver`  
sharelist = \<\<EOS  
\\testserver1\test  
domain\user1 Full Control  
Domain\Admin Full Control  
Domain\chuser Read  
Everyone Read  
domain\ysergeb Read

\\testserver2\BACKUPTEST  
Everyone Change  
BUILTIN\Administrators Read

\\testserver31\DEVQA$  
Everyone Full Control

\\testsercver4\trymeout$  
BUILTIN\Administrators Full Control  
EOS

def find\_share(sharelist, match)  
re = /^#{Regexp.escape(match)}$/  
sharelist.split("\n\n").find { |s| re.match(s) }  
end

puts find\_share(sharelist, “\\testserver2\BACKUPTEST”)

---

<div class="post-metadata">

**Author:** ![anon1m0us](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@anon1m0us](https://www.ruby-forum.com/u/anon1m0us)\
**Post date:** [February 7, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/match-until-new-line/84768/3 "2007-02-07T17:58:07Z")

</div>

> def find\_share(sharelist, match)  
> re = /^#{Regexp.escape(match)}$/  
> sharelist.split("\n\n").find { |s| re.match(s) }  
> end
> 
> puts find\_share(sharelist, “\\testserver2\BACKUPTEST”)

Or more cryptically, but much faster as it’s a single regexp scan and no  
intermediate array is created:

def find\_share(sharelist, match)  
return $1 if /^#{Regexp.escape(match)}$\n(.\*?\n)\n/m =~ sharelist +  
“\n”  
nil  
end

puts find\_share(sharelist, “\\testserver2\BACKUPTEST”)

Note that ^ and $ match start and end of line anywhere within the  
string,  
not just the start and end of the string.

Here find\_share() also strips off the sharename, leaving just the users  
and  
their rights, but you can change this by moving the capture parentheses  
in  
the regexp if you wish.

B.

---

<div class="post-metadata">

**Author:** ![anon1m0us](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@anon1m0us](https://www.ruby-forum.com/u/anon1m0us)\
**Post date:** [February 7, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/match-until-new-line/84768/4 "2007-02-07T17:16:01Z")

</div>

On Feb 7, 8:02 am, “anon1m0us” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am running a server utility that allows me to view server shares and  
> permissions.
> 
> The command I am using is:  
> SHARES=Array.new  
> SHARES=`srvcheck \\testserver.split(“\n”)

A small note, but that first line is totally unnecessary. You don’t  
need to ‘declare’ a variable to be of a particular type before  
assigning it. What you have above is similar to:

foo = 12  
foo = ‘hello world’

The value in your first assignment (a new, empty array instance) is  
totally discarded when you then assign the SHARES constant to a  
completely different array.

---

<div class="post-metadata">

**Author:** ![anon1m0us](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@anon1m0us](https://www.ruby-forum.com/u/anon1m0us)\
**Post date:** [February 7, 2007, 5:59pm UTC](https://www.ruby-forum.com/t/match-until-new-line/84768/5 "2007-02-07T17:59:07Z")

</div>

WHat is sharelist = \<\<EOS ???
