Can Netbeans use regex to make my own 'format code' functions? - regex

I was searching for a function, or a 'format code' function in NetBeans to format my arrays in PHP in a proper way.
E.g.:
$arr1 = array('allow',' => array('index', 'admin', 'view', 'create', 'update', 'delete'),' => array('#'),), array('deny', 'users' => array('*'),);
^^ Is quite hard to read. So if there would be a function to search for:
everytime there is a array( followed by ' replace it with an array(\n'.
or
everytime there is ,\s'\w+' followed by => replace ,\s' with ,\s\n'
Does anyone know if there is a plugin for that? (Btw, I'm not a star in regex so I'm just looking for the array() format problem.)

Related

WP_Query meta_query REGEXP

I am pretty much below beginner level for REGEX/REGEXP and have hit a blocking point in a project I am working in, where I am trying to get the ids for posts that match the search criteria , but I want to restrict the search between 2 sub-strings. I am trying to figure out is how to write the REGEXP in the meta_query:
$args = array(
'post_type'=> 'custom',
'order' => 'DESC',
'posts_per_page' => 10,
'paged' => $page,
'meta_query' => array(
array(
'key' => 'key',
'value' => "title*".$search."*_title",
'compare' => 'REGEXP',
)
),
);
And an example of the field in the DB :
a:302:{s:5:"title";s:10:"Test title";s:6:"_title";s:19:"
Unfortunately none of the combinations I tried based on documentation of SQL REGEXP won't return any values and I am trying to understand how I can pull this off and would appreciate any input.
Also would rather stick to WP_Query for now even though an SQL LIKE "%title%{$search}%_title%" works perfectly , so an alternative solution would be how to set the compare to 'LIKE' and parse it '%' since that is not possible out of the box as the % get escaped I believe.

Laravel Regex Validattion - No ending delimiter '/' found

I don't know why am receiving a:
No ending delimiter '/' found`
Here is a live sample
Here is my regex validations
$validation = Validator::make($request->all(), [
'cpf' => 'required|regex:/[0-9]{11}/',
'identidade' => 'required|regex:/[0-9]{6,11}/'
]);
This one regex:/[0-9]{11}/ is working fine;
But when I put a min delimiter regex:/[0-9]{6,11}/ I get the error.
Anyone know why?
Which version of Laravel are you using?
I'm using 5.8 and it is working fine.
But you can also try to use an array for the rules:
$validation = Validator::make($request->all(), [
'cpf' => ['required', 'regex:/[0-9]{11}/'],
'identidade' => ['required', 'regex:/[0-9]{6,11}/']
]);

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!

MongoDB findOne with regex (security flaw?)

Before i insert the email into the database -> i validate the adress with
if (filter_var($emailAdress, FILTER_VALIDATE_EMAIL))
{
....
}
.. but is this maybe a security flaw?
$userAccObj = $db->user->findOne( array('email' => array('$regex' => '^'.$emailAdress.'$', '$options' => 'i') ));
Schould i do this? or is it not necessary?
$emailAdress= preg_replace("/\#/", '\#', $emailAdress);
$emailAdress= preg_replace("/\-/", '\-', $emailAdress);
$emailAdress= preg_replace("/\./", '\.', $emailAdress);
if (filter_var($emailAdress, FILTER_VALIDATE_EMAIL))
Is a good way to vlaidate an email address in PHP, however, it does use regexes but so far, those have proven to be the best.
$userAccObj = $db->user->findOne( array('email' => array('$regex' => '^'.$emailAdress.'$', '$options' => 'i') ));
The only real problem with that is the . which is a special character which will effect how the regex works, but do you really need to do a regex here? You have checked it is a full email address as such you just need to check for where that exact email address exists (or better yet make a unique index on the field).
As I such I think you can take out the regex and do an exact match.

Regex for finding all href and src and replace them with fully quilifies urls

I need to find all src and href's in a string and replace them with a fully quilifies url.
ex:
src="/test/style.css?somethinghere" becomes src="http://www.mydomain.com/test/style.css?somethinghere"
and the same goes for href's.
I will use them in a web relay server.
I will use regex replace, but what does the regex look like?
You are missing important information, like: what type of regular expression you are interested in. I am assuming that you are referring to perl-compatible regular expression.
In any case this will help in the guidance and theory and answer, and if you let me know the engine/language used I can probably help adapt this to your needs.
I use something similar to collect, pre-process, filter and forward relevant news feeds from various places. MSDN has this problem of relative URIs, here is my configuration for this:
'msdn' => {
'action' => [
{
'tag' => 'get_url_as_content'
},
...
...
{
'regex' => 's/<img( [^\\/>]*)src="\\//<img$1src="http:\\/\\/blogs.msdn.com\\//gsi',
'tag' => 'mod_content'
},
{
'regex' => 's/<a( [^\\/>]*)href="\\//<a$1href="http:\\/\\/blogs.msdn.com\\//gsi',
'tag' => 'mod_content'
}
...
...
]
},
Here you can see the two regular expressions that I am using.
This one restores the content between img and src words and then makes the URLs absolute.
s/<img( [^\/>]*)src="\//<img$1src="http:\/\/blogs.msdn.com\//gsi
Similarly for anchor tags...
s/<a( [^\/>]*)href="\//<a$1href="http:\/\/blogs.msdn.com\//gsi
These have been working for quite some time now, over 3 years without issues :-)
Hope this helps.