Reduce multiple whitespaces to a single space in KDB+/Q - kdb

To get from "a b" to "a b"
ssr["a b";"[ ]+";" "]
doesn't seem to work.
Thanks!

You can use the following which treats each repeating space as a pair,
then using over, 'replaces' these with a single space.
q)x:"This is a test"
q)(" "sv" "vs)/[x]
"This is a test"

It is possible to do this more efficiently then using vs and sv. Using the adverb each-prior ':
{x where not(and':)null x}"This is a test"
"This is a test"
Alternative you can using ssr with the adverb over / in order to continuously remove blocks of two spaces:
ssr[;" ";" "]/["This is a test"]
"This is a test"
The example you provided fails due to the limited regex options available, using + in this sequence "[ ]+" is an example of an operation that is not supported. You can read more about regex in q on the kx wiki.

Related

Microsoft graph Mail Search Strict value

I have an issue with the search parameters. I want to pass a phrase in my query. For exemple i'm looking for emails where the subject is "Test 1".
For this i'm doing a get on this ressource.
https://graph.microsoft.com/v1.0/me/messages?$search="subject:Test 1"
But the behaviour of this query is : Looking for mails that contains "Test" in the subject OR 1 in any other fields.
Refering to the KQL Syntax
A phrase (includes two or more words together, separated by spaces; however, the words must be enclosed in double quotation marks)
So, to do what i want i have to put double quotes (") around my phrase to do a strict value search. Like below
subject:"Test 1"
The problem it's at this point. Microsoft graph api already use double quotes (") after the parameters $search.
?$search="Key words"
So I can't do what is mentioned in the KQL doc.
https://graph.microsoft.com/v1.0/me/messages?$search="subject:"Test 1""
It's throwing an error :
"Syntax error: character '1' is not valid at position 15 in '\"subject:\"test 1\"\"'.",
It's an expected behaviour. I was pretty sure it will not work.
If someone has any suggestions for a solution or a workaround, I'm a buyer.
What I've already tried so far :
Use simple quote
Remove the quotes right after $select=
Remove the subject part $select="Test 1", same behaviour as the first request mentioned in this post. It will looks for emails that contain "test" or "1".
Best regards.
EDIT :
After sasfrog's anwser :
I used $filter : It works well with simple operator AND, OR.I have some errors by using the Not Operator. And btw you have to use the orderby parameter to show the result by date and add the field in filter parameters.
Exemple 1 (working, what I asked for first) :
https://graph.microsoft.com/v1.0/me/messages/?$orderby=receivedDateTime desc &$filter=receivedDateTime ge 1900-01-01T00:00:00Z AND contains(subject,'test 1')
Exemple 2 (not working)
https://graph.microsoft.com/v1.0/me/messages/?$orderby=receivedDateTime desc &$filter=(receivedDateTime ge 1900-01-01T00:00:00Z AND contains(subject,'test 1')) NOT(contains(from/EmailAddress/address,[specific address]))
EDIT 2
After some test with the filter parameters.
The NOT operator is still not working so to workaround use "ne" (non-equals)
the example 2 becomes :
https://graph.microsoft.com/v1.0/me/messages/?$orderby=receivedDateTime desc&$filter=(receivedDateTime ge 1900-01-01T00:00:00Z AND contains(subject,'test 1')) AND (from/EmailAddress/address ne [specific address])
UPDATE : OTHER SOLUTION WITH $search
Using $filter is great but it looks like it was sometimes pretty slow. So I found a workaround aboutmy issue.
It's to use AND operator between all terms.
Exemple 4 :
I'm looking for the mails where the subject is test 1;
Let value = "test 1". So you have to splice it by using space separator. And after write some code to manipulate this array, to obtain something like below.
$search="(subject:test AND subject:1)"
The brackets can be important if you use a multiple fields search. And Voilà.
Not sure if it's sufficient for what you're doing, but how about using the contains function within a filter query instead:
https://graph.microsoft.com/v1.0/me/messages?$filter=contains(subject,'Test 1')
Sounds like you're already looking at the doco but here it is just in case.
Update also, this worked for me using the search method:
https://graph.microsoft.com/v1.0/me/messages?$search="subject:'Test 1'"

Adding/Removing from String kdb

I was doing some work to do with kdb and have been tinkering with strings and variables. I was just wondering if its possible to remove part of a string and add something to do the end of it.
s1:"Hello" s2:" World"
I have a joint string "Hello World" which I created using
s3:s1,s2
I was trying to remove the Hello and add something after the World in the joint string.
s3[1*til 6] = Hello
This allows me to select the Hello part of the string if this helps
you could use drop (_) to get rid of the "Hello" and join (,) to add on what you want. Something like
q)6_s3,"star Hiphop"
"Worldstar Hiphop"
If you didn't want to count the letters in the first word you could use vector from scalar (vs) to get a list of enlisted words and index into it, then join onto that:
q)(" " vs s3)[1],"star Hiphop"
"Worldstar Hiphop"
Hope this helps.
Strings are character lists, so the drop function _ will still work on them. For example 1_"Hello" will return ello.
So if you want to remove "Hello" from your string s3 you would use
q)5_s3
"World"
Adding something onto the end of this then requires the join operator ,, for example
q)s:"HelloWorld"
q)s1:"Mr. "
q)s2:5_s
q)s3:"wide"
q)s1,s2,s3
"Mr. Worldwide"
You could use the ssr function (string search replace).
q)s3:"HelloWorld"
q)ssr[s3;"Hello";""], " of War"
"World of War"

How to replace similar code in VS 2017.

I am replacing our logging functionality and it is taking a long time to manually go through all of the code and replace it.
Here is the current code:
Error Messages:
cLogger.LogMessage(ComponentID.ClientID, CLASS_NAME, "AddContextMenuItem", MessageType.mtErrorMessage, "Null MenuItem provided. MenuItem's status not changed");
cLogger.LogMessage(ComponentID.ClientID, CLASS_NAME, "enableDisableToolbarItem", MessageType.mtErrorMessage, "Invalid toolbaritem provided.");
Exceptions:
cLogger.LogMessage(ComponentID.ClientID, CLASS_NAME, "enableDisableContextMenuItem", MessageType.mtException, ex);
cLogger.LogMessage(ComponentID.ClientID, CLASS_NAME, "AddToolbarItem", MessageType.mtException, exc);
Is there a simple way to create a macro (never used a macro before) or power shell or notepad++ script or something else to find and replace all of these different instances so that they look like the following:
New Error Messages:
logger.Log(LogLevel.Error, CLASS_NAME + " AddContextMenuItem - Null MenuItem provided. MenuItem's status not changed");
logger.Log(LogLevel.Error, CLASS_NAME + " enableDisableToolbarItem - Invalid toolbaritem provided.");
and
New Exceptions:
logger.Log(LogLevel.Exception, CLASS_NAME + " enableDisableContextMenuItem - " + ex);
logger.Log(LogLevel.Exception, CLASS_NAME + " AddToolbarItem - " + exc);
I am replacing the code in the entire project and it will just simply take way too long to go through and manually change all of the logging code manually. Any help is greatly appreciated.
There are a few options:
Regex Search & Replace in Visual Studio:
search for the exception example
\w+logger.LogMessage\([^,]+,([^,]+),([^,]+),[^,]+,([^\",]+)\);
replace
logger.Log(LogLevel.Exception, $1 + $2 + $3);
Use Resharper structural Search & Replace
Build a CodeFix for Roslyn
Yes, you can likely do this with a Regular Expression, easier in PowerShell perhaps than in Notepad++ or perhaps VSCode.
It's difficult to tell from your examples precisely what you are changing in each item, but the basic concept is to do the following:
Match the static text that establishes the type of item to change
Also match the variable text with wildcards (.* etc) enclosed in CAPTURING parentheses
Replace with new static text and 'rearranged' variable text using the $1, $2, etc backreferences to the capture groups (or $Matches[1] etc.)
If #3 is more complicated, you'll need to further alter the variable text before replacing -- this is where a script language has an advantage over a pure search and replace.
Here is a simplified example (PowerShell but similar in other langauges or editors that support Regexes) for statically replacing the "FunctionOldName" while swapping the order of Param1 and Param2 and altering the names based on the original names for these params:
"Function_OldName Param1 Param2" -replace 'Function_OldName\s+(\w+)\s+(\w+)',
'NewFunctionName New$2Parm New$1Parm'
The $1 and $2 are backreferences to the "1st parens" and "2nd parens" respectively in the match string.
If you can write out clear examples showing which parts of your changed text must be matched, simply altered, rearranged, or rebuilt then it might be possible to show you some more relevant examples....
You can do this across many files with either PowerShell or the editors, but generally doing it to many files is again a bit easier in a Programming language (e.g., PowerShell.)
Get-ChildItem *.PS1 -recurse | ForEach-Object {
'Function_OldName\s+(\w+)\s+(\w+)', # your match goes here
'NewFunctionName New$2Parm New$1Parm' # your replacement goes here
}

Mysterious " " in Swift 4 print output (Xcode 9Beta)

I believe this is my first post here. I am teaching myself Swift and have come across some odd behavior involving the mysterious appearance of a leading " " in a print statement. I was exploring print formatting and this code is producing a leading " " in the first dashedLine printed.
Code:
var dashedLine = "-------------------------------------------------------------------"
print("a bunch of text\n", dashedLine)
print(dashedLine)
Output:
a bunch of text
-------------------------------------------------------------------
-------------------------------------------------------------------
Why the leading space before the first dashed line?
I've read the Swift 4 documentation. (In playing with "terminator" syntax at the end of a print list, I get unanticipated results, including suppression of output, depending.) I am curious as to the appearance of the leading space as my primary question.
By default, a print statement with multiple arguments prints those out with a space in between.
You can find more in Apple's documentation here.
Following on to #bajracharyas353’s answer, a solution if you’re needing to avoid this would be to combine strings using any of the methods Swift allows, like "a" + "b" or String.append, or print(String1, String2, separator: "").
As for suppression of output, I think I’ve run into the same thing with JWTs. There seems to be a pretty modest limit on output, but I could be wrong there.
Problem
The Swift.print(_ items: Any...) function prints multiple arguments separated by a space.
Solution
Use print("a bunch of text\n", dashedLine, separator: "") instead

regexpression [R] "About 52,883,038 results"

I want to parse an html webpage (Specifically a Google Search Results Page)
Looking for the specific counter string
"About *many results"
where *many can range from 0 to 999,999,999,999 results
grep("About [0-9] results",file)
I can't figure out how to incorporate the range of numbers (including commas) into the regular expression. Can anyone clarify? I've looked for similar questions posted, but their codes do not work for this task.
I'm guessing introduce some kind of wildcard "." but I don't think I'm using it correctly
The structure I had in mind was
Any#Times { { Any#Times( [0-9] ) },}
Solved own question...
didn't have to be fancy at all
"About .* results"
works fine
Depending on the content of the page then your .* works, but could get a very long and incorrect string.
If you want to make sure that you get only numbers, try:
"About ([0-9]+|[0-9]{1,3}(,[0-9]{3})*) results"
I've tested it with grep -E and it'll give you ungrouped numbers:
About 10000000 results
as well as grouped numbers using British/English conventions:
About 100,000 results
but not non-numbers:
About a bajillion results
or badly grouped numbers:
About 100,0 results