i'm stuck to write a regex pattern to the below lines, my problem is to match it in multilines
* History of Modifications:
* $Log: /System/Utilities.cpp $
*
* 5 7/12/22 4:49p Peter
*
* 4 5/12/22 5:57p Mina
*
* 3 20/11/22 6:15p Simon
*
* 2 18/10/22 1:48p Micheal
*
* 1 5/10/21 4:13p Peter
*
*/
these lines always start with
$log: filename $
until the comment ends with */
i tried these two regex
^ \* \d+ .*
/\d{1,2}\/\d{1,2}\/\d{2} \d{1,2}:\d{2}[ap] [A-Z][a-z]+/
My regex:
^( *)((?:[*+-]|\d+\.)) [\s\S]+?(?:\n{2,}(?! )(?!\1(?:[*+-]|\d+\.) )\n*|\s*\n*$)
Data, match successfully:
* 2
* 3
Data, cannot match:
<--- new line break here
* 2
* 3
Data, cannot match:
Hello <--- new line break here
* 2
* 3
Desired result for all three cases:
match:
* 2
* 3
You should use the multiline flag. For the examples you have provided, the following regex would work:
/^[*+-] (.*)$/m
This will match any lines starting with *, + or -.
How do I check the cron entry 0 5 * * * /usr/bin/aide --check with a regex? I would like to check this in Chef InSpec like
its('content') { should match /<the regular expression>/ }
describe cron do
it { expect(subject).to have_entry '0 5 * * * /usr/bin/aide --check' }
end
is the proper way to do this in Serverspec and will also solve your problem with formatting immediately.
If you really wanted to use a regexp (and your followup comment left as an answer implies you don't), then you could do:
its(:content) { is_expected.to match %r{0 5 \* \* \* /usr/bin/aide --check} }
The regex could be /0 5 * * * \/usr\/bin\/aide --check/
I'm trying to figure out a regex to validate what follows one of (<, >, =, !=) to insure that it is either an integer or a single-quoted-string (possible with spaces).
Some examples:
SELECT * WHERE field1=5 // OK
SELECT * WHERE field1= 5 // OK
SELECT * WHERE field1< -2 // OK
SELECT * WHERE field1='5' // OK
SELECT * WHERE field1=a // NOT OK - a is not an integer
SELECT * WHERE field1!='a' // OK
SELECT * WHERE field1='a // NOT OK - missing closing quote
EDIT: I forgot to add it should work with multiple fields such as
SELECT * WHERE field1=5 AND field2=b // NOT OK since field2 following is not a int
SELECT * WHERE field1=5 AND (field1<6 AND fieldb='a') // OK
.*(=|<|>|!=)\s*(-?\d+|'[^']+')
regex101
I have the following string:
cn=abcd,cn=groups,dc=domain,dc=com
Can a regular expression be used here to extract the string after the first cn= and before the first ,? In the example above the answer should be abcd.
/cn=([^,]+),/
most languages will extract the match as $1 or matches[1]
If you can't for some reason wield subscripts,
$x =~ s/^cn=//
$x =~ s/,.*$//
Thats a way to do it in 2 steps.
If you were parsing it out of a log with sed
sed -n -r '/cn=/s/^cn=([^,]+),.*$/\1/p' < logfile > dumpfile
will get you what you want. ( Extra commands added to only print matching lines )
/^cn=([^,]+),/
Also, look for a pre-built LDAP parser.
Yeah, using perl/java syntax cn=([^,]*),. You'd then get the 1st group.
I had to work that out in PHP.
Since a LDAP string can sometimes be lengthy and have many attributes, I thought of contributing how I am using it in a project.
I wanted to use:
CN=username,OU=UNITNAME,OU=Region,OU=Country,DC=subdomain,DC=domain,DC=com
And turn it into:
array (
[CN] => array( username )
[OU] => array( UNITNAME, Region, Country )
[DC] => array ( subdomain, domain, com )
)
Here is how I built my method.
/**
* Read a LDAP DN, and return what is needed
*
* Takes care of the character escape and unescape
*
* Using:
* CN=username,OU=UNITNAME,OU=Region,OU=Country,DC=subdomain,DC=domain,DC=com
*
* Would normally return:
* Array (
* [count] => 9
* [0] => CN=username
* [1] => OU=UNITNAME
* [2] => OU=Region
* [5] => OU=Country
* [6] => DC=subdomain
* [7] => DC=domain
* [8] => DC=com
* )
*
* Returns instead a manageable array:
* array (
* [CN] => array( username )
* [OU] => array( UNITNAME, Region, Country )
* [DC] => array ( subdomain, domain, com )
* )
*
*
* #author gabriel at hrz dot uni-marburg dot de 05-Aug-2003 02:27 (part of the character replacement)
* #author Renoir Boulanger
*
* #param string $dn The DN
* #return array
*/
function parseLdapDn($dn)
{
$parsr=ldap_explode_dn($dn, 0);
//$parsr[] = 'EE=Sôme Krazï string';
//$parsr[] = 'AndBogusOne';
$out = array();
foreach($parsr as $key=>$value){
if(FALSE !== strstr($value, '=')){
list($prefix,$data) = explode("=",$value);
$data=preg_replace("/\\\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\\1')).''", $data);
if(isset($current_prefix) && $prefix == $current_prefix){
$out[$prefix][] = $data;
} else {
$current_prefix = $prefix;
$out[$prefix][] = $data;
}
}
}
return $out;
}