last day (15 days later) » 

4:55 PM
0
A: match one of several strings

nu11p01n73RYou can write a single regular expression for this ^(function)? ?\w+? ?(\(\))?\n? ? ?\{ will match all the four of function xyz () { function xyz () { xyz () { { also make sure you add a multiline m modifier DEMO

 
thanks, but should xyx be something like this (\W+)? ?(()) as it might be different letters so I change it to: if ($match=~ m/(function)? ?(\w+)? ?(())?\n? ? ?\{/) {....also what if I want to exit the loop when mact is found and go the the next file, I used this line "return $1 if defined $1;" and it doesn't exit the loop thanks again..
 
yeah..you are correct...i was thinking only of your example....corrected the answer thred
you are always welcome :) glad it worked.
 
thanks I thought I have it right using your recommendation but it just returns a few files not all the files, this is due to some of the file has "_" on the name such function xyz_abc or abc_xyz_abc_xyz, how can catch those?
 
it works fine...see the demo
 
not sure why it doesn't work for me, i tried the abc_x_abc() on demo and there was no match
 
4:55 PM
see the full demo If that doesnt work try replacing \w with [a-z_]
 
thanks I think the problem is it is matching string only if ends with "{" else it won't match say abc_abc ()
 
i was checking for the newline correction
 
thanks, I think there is something else wrong in my script as I do have a files that lists all files on remote server, I do ssh to remote server and search all files listed on the file and search for that match you have provided and it returns in my case only a few and I'm sure it has a lot more!
 
http://regex101.com/r/sX1sZ0/1
Check this i think this is more correct to much newline character.
it doesnt returns because in my first regex i didnt consider spaces after () which can occure before the new line character.
 
you mean /^(function)? ?\w+? ?(())?\n *\{/
and that does not match any!
 
5:07 PM
did you check the demo??
 
yes, the one /^(function)? ?\w+? ?(())?\n *\{/ right?
 
sorry :P
1 min let me correct that
 
no worries and I do much appreciated for your time
 
this is the correct one. ^(function)? ?\w+? ?(())? *\n?\{
 
sorry same as before it returns only a few
 
5:15 PM
:(
kk can you show me one such file content
 
it does not return if the string is say abc ()
only if it has "_" on the in, I thtink
 
sorry and if there no space say zbc_abs() then no match so the string good also have space before () or no space
in other words it won't match this one: function abc_yzx_abs()
 
you mean the spaces are optional?
 
yes
 
5:24 PM
kk
 
i know :-)
it works only if it ends with "{" like this function abc_yzx_abs() { and it won't for function abc_yzx_abs()
 
for this one ^(function)? ?\w+? ?(())? *\n?\{
i will give examples
 
thanks, I think as said before something is not right in my code as it return only a few perhaps it gets time out while parsing the file, here is my script, it ssh to the remote server and issue a command in which it list all files with extension .sh then put it into the file, open the file and read each line and search for match, the regex you have provided and iI think in some point it gives up and exit and returns only a few...here is the code:
$ssh = Net::SSH::Expect->new (
'host' => "$pan",
'user' => "$user",
'password' => "$pass",
'ssh_option' => '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -v -v -v',
'timeout' => 3 ,
'raw_pty' => 1,
);
#Starting SSH process
$ssh->run_ssh() or die "SSH process couldn't start: $!";
my $login_output = $ssh->login();
my $find = $ssh->exec("find / -iname '*.sh'");
print OUT $find;
close $info;
}
print "\n";
my $count = 0;
my $file= "files1.txt";
open my $info, $file or die "Could not open $file: $!";
 
5:44 PM
am not that expert in perl
but will try a hand
 
thanks..
 
so the $match reads each line from the file right?
say something like function abc_yzx_abs()
{
 
yes I assume, but it returns just a few and I assume it exits due to timeout or...
 
so here there is a problem
when it reads line by line, it just read function abc_yzx_abs() this much only
and checks for the pattern which obviously would fail
because { is in the next line which is read only in the next pass
 
I assume as I'm not that good in regex
 
5:56 PM
its not of regex, isn't it the logic in perl you have written
in each iteration it reads a single line right?
 
the first loop reads file that contains list of files and location:
while( my $line = <$info>)
 
the second while loop open the file and macth:
while( my $match = <$error>) {
if ($match=~ m/^(function)? ?\w+? ?(\(\))? *\n?\{/) {
I should say the nested while loop while( my $match = <$error>) {
if ($match=~ m/^(function)? ?\w+? ?(\(\))? *\n?\{/) {
 
so the entire content of file is in $match just at once??
that is the second while reads each line from the file opened by the first while
am i correct?
 
right if there is match then i returns to parent loop and read next file
correct
 
6:01 PM
so say the content of file is like
function abc_yzx_abs()
{
some text
here for the first iteration only function abc_yzx_abs() is read
in second iteration of while loop { is read and in third some text is read and so on
right
hope you understand
 
first iteration opens a file, then the second iteration reads each line from that file as soon it it finds any match from regex then it print the match and return to first iteration loop and it open another file..
 
i think you misunderstood my iteration with the while loop
when i said iteration, i meant excecution step of the second while loop
when i said iteration, i meant excecution step of the second while loop
 
not sure what I mean, second while loop just read the file that has been opened by first while and if match then returns to the first while loop..did answer your question!
 
i understood that...but isnt the second loop read a single line at a time
 
that is correct, I can't thing of any other way, but correct me if I'm wrong but I need to read each line right then if it matches then it exit the loop!
 
6:13 PM
yeah you are correct it reads a single line from the file
now
function abc_yzx_abs() {
for this there is a match and the loop exits as we excpect
ok?
but imagine what will happen when the loop reads
function abc_yzx_abs()
by our regex it doesnt matches this line
 
Hummm, say if the first match is function abc_yzx_abs() then it returns same for
function abc_yzx_abs() {..ami right?
 
for abc_yzx_abs() { it wont be much issue as { occures in the same line.
will give you a work around ;)
try this regex
^(function)? ?\w+? ?(())? ?\{?
am not sure if it matches all
but lets try
to be more precise add an ending anchor as well
^(function)? ?\w+? ?(())? ?\{?$
 
so you think I shuold use this one instead
^(function)? ?\w+? ?(())? ?\{?$
 
its short cut
but if you ask me it serves the purpose
but we are supposed to check the syntax of the function right
it would match even if { is missing in the second line...that is the file you are checking contains syntactial errors.
 
correct, I tried the first one "^(function)? ?\w+? ?(())? ?\{?" and it returns 50 and second one just 10..so you think I should go with first one?
 
6:27 PM
go with the first.
;)
 
my bad it does not exit when it matches the 50 was from just 3 files :-(
 
the second got wrong may be because of blank spaces at the end of the line
we cannot trust the spaces as such
what if the file contains more number of spaces than we expect
 
that is possible too
 
if that is the case then i modified all the + with *
^(function)? *\w+? *(())? *\{? *$
demo
 
thanks it seems it covers all that we have discussed, but not sure why it does not exit from the loop when it find first match as I see a duplicate file is reported
 
6:36 PM
:)
 
thank you so much I look to see why it does not exit...MUCH MUCH MUCH MUCH appreciated in advance.. and I can call you a friend ;-)
 
you are always welcome my friend :)
 
;-)
 
as for exit, why dont you use a single return??
rather than return $1 if define
 
let me try that
it returns error saying Can't return outside a subroutine
 
6:41 PM
ohhh what am i thinking
its break right not retrun
foolish of me
 
no worries ;-)
 
did that work??
 
you know i tried your demo and it matches abc too without any () or {, so it is not good as it may returns not valid match ...
I guess that's why it returns more :-(
 
more problems :(
 
:-(
 
6:47 PM
corrected :D
^(function)? *\w+? *() *\{? *$
actually there is some back slashes which may not apear in the chat
will give you the demo link
how is this
 
better..thanks again my firend
 
worked??
 
let me look as there are many more than 20k files that it has to match and it returns only 4, I will look other in random to see if that matches your regex..stay tune..
I think and believe your regex is correct and there is something else in my code as it does not return all files, just a few in this case only 4 out of $20k files, I looked at one and script didn't pick that file and I put it into demo and it matched..so not sure what is going on...thanks very much I look to see if I can get around this..thanks for your time and have a great weekend my friend..
 
7:05 PM
:( should have worked more....any way good weekend friend...let me know when you worked around :) happy hacking
 
thanks and like wise....
 

last day (15 days later) »