How with pint can I remove space after negative “!” symbol? - laravel

How with laravel/pint 1.4 can I remove space after negative “!” symbol ?
Not :
if (! self::$wasSetup) {
But :
if (!self::$wasSetup) {
I suppose that last is psr-12 rule...
Thanks!

As per PHP-CS-Fixer , Rule not_operator_with_successor_space
Logical NOT operators (!) should have one trailing whitespace.
So by default, one trailing whitespace is enabled. To disable white space after NOT operators then you should create a pint.json file in the root folder
{
"preset": "laravel",
"rules": {
"not_operator_with_successor_space": false
}
}

Related

elastic search regex query to search string starts with number

i am not able to search string start with number
PUT music/song/1?refresh
{
"suggest" : [
{
"input": "123hello",
"weight" : 3
}
]
}
i have tried the following regex query
POST music/_search?pretty
{
"suggest": {
"song-suggest" : {
"regex" : "^[0-9].*$",
"completion" : {
"field" : "suggest"
}
}
}
}
You should try [0-9]*.*.
The point is that lucene regexp doesn't use '^' and '$' as symbols of start and end of the string to anchor your pattern. In fact regexp in lucene is anchored to whole string by default, see:
Most regular expression engines allow you to match any part of a string. If you want the regexp pattern to start at the beginning of the string or finish at the end of the string, then you have to anchor it specifically, using ^ to indicate the beginning or $ to indicate the end.
Lucene’s patterns are always anchored. The pattern provided must match the entire string.
See also my very similar question. Especially if your field could be more then 256 characters.
Don't know if question is still actual, so I just leave it here.

Replace brackets for negative numbers with hyphen

I'm processing a list of numbers (as strings) in Ruby:
"4.3", "2.1", "(0.1)", "(3.4)"
When I process, I'd like to leave the positive numbers alone. For the negative numbers, I'd like to remove the brackets and insert a hyphen (keeping them as strings is fine):
"4.3", "2.1", "-0.1", "-3.4"
I've stripped the brackets with gsub:
"(0.1)".gsub(/[()]/, "")
But adding the hyphen to signify the negative causes problems, as this:
"(0.1)".gsub(/[()]/, "-\1")
... supposedly replaces both brackets, and puts two hyphens, along with an unknown character.
I'm not married to the gsub/regex approach, so anything that works quickly would work.
["4.3", "2.1", "(0.1)", "(3.4)"]
.map{|s| s =~ /\((.+)\)/ ? "-#$1" : s}

Elasticsearch Regex Query

I am running elasticsearch v1.1.1 and I am having trouble getting results from regex searches.
{
"query" : {
"regexp" : {
"lastname" : "smit*"
}
}
}
Returns 0 results (when I know I have 'smith' in the data.
I have also tried:
{
"query" : {
"filtered" : {
"filter" : {
"regexp" : {
"lastname" : "smit*"
}
}
}
}
}
Any help would be appreciated.
So first off, a lot of this is dependent on how you indexed the field - analyzed or not, what kind of tokenizer, was it lowercased, etc.
To answer your specific question concerning regexp queries, assuming your field is indexed as "smith" (all lower case) you should change your search string to "smit.*" which should match "smith". "smit." should also work.
The reason is that in regexp (which is different than wildcard) "." matches any character. "*" matches any number of the previous character. So your search would match "smitt" or "smittt". The construct ".*" means match any number (including 0) of the previous character - which is "." which matches any. The combination of the two is the regexp equivalent of the wildcard "*".
That said, I'd caution that regexp and wildcard searches can have significant performance challenges in text indexes, depending upon the nature of the field, how it's indexed and the number of documents. These kinds of searches can be very useful but more than one person has built wildcard or regexp searches tested on small data sets only to be disappointed by the production performance. Use with caution.
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html
ElasticSearch Regexp Filter

Unescape an escaped string

I have a string like
rule = ".radio-inline,\n.checkbox-inline {\n display: inline-block;\n}"
The string contains special characters like \n. How can I return the part of the string before {}?
For the above string it should return ".radio-inline,\n.checkbox-inline "
I tried to write it using
Regexp.escape(rule).match(/(.*){/)[1]
The problem is that it returns:
"\\.radio\\-inline,\\n\\.checkbox\\-inline\\ \\"
This should do it:
rule = ".radio-inline,\n.checkbox-inline {\n display: inline-block;\n}"
rule[/.*(?={)/m]
#=> ".radio-inline,\n.checkbox-inline "
.* matches zero or more characters from the beginning of the line. Being greedy by default, it matches as many characters as it can.
(?={), a positive lookahead, requires the character { to immediately follow the matched string, but it is not part of the match.
m stipulates that the match is to be made over multiple lines.
This uses the form of the method String#[] that takes a regex as an argument.
This regex gets you the part befor {}:
[^"{}]+(?={.*})
Try this demo
However it doesn't select you the " at the and and beggining, I hope this will work for you

PregMatch . space and #?

Can someone tell me, what's wrong in this code:
if ((!preg_match("[a-zA-Z0-9 \.\s]", $username)) || (!preg_match("[a-zA-Z0-9 \.\s]", $password)));
exit("result_message=Error: invalid characters");
}
??
Several things are wrong. I assume that the code you are looking for is:
if (preg_match('~[^a-z0-9\h.]~i', $username) || preg_match('~[^a-z0-9\h.]~i', $password))
exit('result_message=Error: invalid characters');
What is wrong in your code?
the pattern [a-zA-Z0-9 \.\s] is false for multiple reasons:
a regex pattern in PHP must by enclosed by delimiters, the most used is /, but as you can see, I have choosen ~. Example: /[a-zA-Z \.\s]/
the character class is strange because it contains a space and the character class \s that contains the space too. IMO, to check a username or a password, you only need the space and why not the tab, but not the carriage return or the line feed character! You can remove \s and let the space, or you can use the \h character class that matches all horizontal white spaces. /[a-zA-Z\h\.]/ (if you don't want to allow tabs, replace the \h by a space)
the dot has no special meaning inside a character class and doesn't need to be escaped: /[a-zA-Z\h.]/
you are trying to verify a whole string, but your pattern matches a single character! In other words, the pattern checks only if the string contains at least an alnum, a space or a dot. If you want to check all the string you must use a quantifier + and anchors for the start ^ and the end $ of the string. Example ∕^[a-zA-Z0-9\h.]+$/
in fine, you can shorten the character class by using the case-insensitive modifier i: /^[a-z0-9\h.]+$/i
But there is a faster way, instead of negate with ! your preg_match assertion and test if all characters are in the character range you want, you can only test if there is one character you don't want in the string. To do this you only need to negate the character class by inserting a ^ at the first place:
preg_match('/[^a-z0-9\h.]/i', ...
(Note that the ^ has a different meaning inside and outside a character class. If ^ isn't at the begining of a character class, it is a simple literal character.)

Resources