CSR Subject with Special Characters extract - regex

I would need to extract the $subj as seen below snippet, but looks like my Regex isn't working as expected. This is actually similar with this: How to string manipulate/extract subject contents in a CSR using OpenSSL command + Perl? but with a different subject entry in the CSR. I'm not sure if I did a good thing in my regex for the %subjinfo
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my $subj =
'subject=/O=test~##$^()_+-=\{}|;':",./<>/OU=test~##$^()_+-=\{}|;':",./<>/emailAd‌​dress=test~##$^()_+-=\{}|;':",./<>/L=CDE/ST=ABC/C=AU/CN=test~##$^()_+ -=\{}|;':",./<>';
my %subjinfo = ( $subj =~ m,(\w+)=([^=]*)(?:/|$),g );
print Dumper \%subjinfo;
and so must give a result to this:
$VAR1 = {
'subject' => '',
'L' => 'NYC',
'C' => 'AMER',
'OU' => 'test~##$^()_+-=\{}|;':",./<>',
'emailAddress' => 'test~##$^()_+-=\{}|;':",./<>',
'ST' => 'AMER',
'CN' => 'test~##$^()_+-=\{}|;':",./<>',
'O' => 'test~##$^()_+-=\{}|;':",./<>'
};
Is that possible? Can you advise?

Splitting on regex looks more natural than regex only solution,
use Data::Dumper;
my $subj = q(subject=/O=test~##$^()_+-=\{}|;':",./<>/OU=test~##$^()_+-=\{}|;':",./<>/emailAddress=test~##$^()_+-=\{}|;':",./<>/L=CDE/ST=ABC/C=AU/CN=test~##$^()_+ -=\{}|;':",./<>);
(undef, my %subjinfo) = split m|/?(\w+)=|, $subj;
print Dumper \%subjinfo;
output
$VAR1 = {
'emailAddress' => 'test~##$^()_+-=\\{}|;\':",./<>',
'CN' => 'test~##$^()_+ -=\\{}|;\':",./<>',
'OU' => 'test~##$^()_+-=\\{}|;\':",./<>',
'L' => 'CDE',
'C' => 'AU',
'ST' => 'ABC',
'subject' => '',
'O' => 'test~##$^()_+-=\\{}|;\':",./<>'
};

Related

Perl deferred interpolation of string

I have a situation where there is a triage script that takes in a message, compares it against a list of regex's and the first one that matches sets the bucket. Some example code would look like this.
my $message = 'some message: I am bob';
my #buckets = (
{
regex => '^some message:(.*)',
bucket => '"remote report: $1"',
},
# more pairs
);
foreach my $e (#buckets) {
if ($message =~ /$e->{regex}/i) {
print eval "$e->{bucket}";
}
}
This code will give remote report: I am bob. I keep looking at this and feel like there has to be a better way to do this then it is done now. especially with the double quoting ('""') in the bucket. Is there a better way for this to be handled?
Perl resolves the interpolation when that expression is evaluated. For that, it is sufficient to use a subroutine, no eval needed:
...
bucket => sub { "remote report: $1" },
...
print $e->{bucket}->();
Note that you effectively eval your regexes as well. You can use pre-compiled regex objects in your hash, with the qr// operator:
...
regex => qr/^some message:(.*)/i,
...
if ($message =~ /$e->{regex}/) {
You could use sprintf-style format strings:
use strict;
use warnings;
my $message = 'some message: I am bob';
my #buckets = (
{
regex => qr/^some message:(.*)/,
bucket => 'remote report: %s',
},
# more pairs
);
foreach my $e (#buckets) {
if (my #matches = ($message =~ /$e->{regex}/ig)) {
printf($e->{bucket}, #matches);
}
}

Laravel regex validation for empty string

I have the following validation,
it should match string with letters,numbers,dashes. And empty input should also be valid.
The normal string validation is ok, but I can not make it match "empty" input.
'letter_code' => 'regex:/^[A-Za-z0-9\-]*$/'
letter_code format is invalid
tests:
"C14" // valid
"3.14" // "format is invalid", as expected
"-" // valid
"" // "format is invalid", NOT expected
I just found out in laracasts forum, that there is a nullable rule.
You can read about it in the official docs.
Without the nullable rule empty strings are considered invalid if there is a regex rule as well.
If you don't add required as additional validator empty string must pass
Here is phpunit test:
/** #test */
public function letterCode()
{
$trans = new \Illuminate\Translation\Translator(
new \Illuminate\Translation\ArrayLoader, 'en'
);
$regex = 'regex:/^[A-Za-z0-9\-]*$/';
$v = new Validator($trans, ['x' => 'C14'], ['x' => $regex]);
$this->assertTrue($v->passes());
$v = new Validator($trans, ['x' => '3.14'], ['x' => $regex]);
$this->assertFalse($v->passes());
$v = new Validator($trans, ['x' => '-'], ['x' => $regex]);
$this->assertTrue($v->passes());
$v = new Validator($trans, ['x' => ''], ['x' => $regex]);
$this->assertTrue($v->passes());
}
This is tested with Laravel 5.5
I was facing the same problem in Laravel 7.x using GraphQL Types. I needed something similar to this in a field called phone or nothig (empty string). So I did something like this:
'phone' => [
'name' => 'phone',
'type' => Type::string(),
'rules' => ['regex:/^[A-Za-z0-9\-]*$/','nullable']
Here, phone is the name of the field you want to enter text into, and rules is what we define as regex, or NULL.
Hope this helps someone!

How to use regex optionally filter out some word at the end

I have strings like this
/city/town/movie/M-12345
/city/town/movie/M-23456/shown
/city/town/movie/M-34567/coming
i would like to have a regex to filter out movie id if the path is end with id
(?<path1>.*)(?:(\/M-[0-9]+$))
would like path1 to be
/city/town/movie
/city/town/movie/M-23456/shown
/city/town/movie/M-34567/coming
however actual result is only
/city/town/movie
any idea what i did wrong?
Use the following pattern:
^.+(?=\/M-\d+$)|^.+(?!\/M-\d+$)
https://regex101.com/r/F76E3f/1
Pretty simple like this if you're using PHP:
<?php
$urls = array('/city/town/movie/M-12345','/city/town/movie/M-23456/shown','/city/town/movie/M-34567/coming');
$regex = '~M-\d+$~';
$cleaned = preg_replace($regex, '', $urls);
print_r($cleaned);
/*
Array
(
[0] => /city/town/movie/
[1] => /city/town/movie/M-23456/shown
[2] => /city/town/movie/M-34567/coming
)
*/
?>

Laravel regex validation strange behavior

When i am using preg_match to validate a input it behaves as i want.
$str = "abc-'xx";
$is_valid = preg_match("/[^A-Za-z'-]/", $str); // output => true
But when I am using laravel validation
$rules = [
'name' => "regex:/[^A-Za-z'-]/"
];
OR
$rules = [
'name' => ["regex:/[^A-Za-z'-]/"]
];
it always failed.
Why? and How can i solve this?
I was able to solve this by using regex /^[A-Za-z\-\']+$/
$rules = [
'name' => ["regex:/^[A-Za-z\-\']+$/"]
];
But couldn't find a reason, Appreciate someone can explain this.

Detect Fields that do not have ID attribute using grep and regex [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Need to find the following patern in a file using grep and regular expression :-
$form->AddFields({
type => 'hidden',
name => 'xyz',
value => 0,
});
i think we nee to use multi line grep for this.
Can anyone help please.?
Using awk:
awk '{ printf /id =>/? FS: $0 RS}' RS='});' file
Input:
$form->AddFields({
id => 2048
type => 'hidden',
name => 'xyz',
value => 0,
});
$form->AddFields({
type => 'hidden',
name => 'xyz',
value => 0,
});
$form->AddFields({
type => 'hidden',
name => 'xyz',
id => 1024,
value => 0,
});
Output:
$form->AddFields({
type => 'hidden',
name => 'xyz',
value => 0,
});
It's not a grep statement, but might want to try this Perl script (this assumes you're feeding the input via STDIN). It also assumes that you don't have any forms that are truncated within the input (forms that begin before the input start or end after the input ends), but there's ways to deal with those conditions if they are relevant to you.
use strict;
use warnings;
sub processRecord {
my ($record) = #_;
my $text = join("\n", #{$record});
print "$text\n" unless $text =~ /ID\s*=>\s*/;
}
my $recordStart = qr/^\s*\$form\s*->\s*AddFields\s*\(\s*\{\s*/;
my $recordEnd = qr/\s*\}\s*\)\s*;\s*$/;
my #record;
my $inRecord = 0;
while (my $line=<>) {
next unless $line =~ /\S/;
chomp($line);
$inRecord = 1 if $line =~ /$recordStart/;
if ($line =~ /$recordEnd/) {
push(#record, $line);
processRecord(\#record);
#record = ();
$inRecord = 0;
next;
}
push(#record, $line) if $inRecord;
}