regex expression to replace everything in a string between 2 like characters? - regex-group

This seems simple but I am not very familiar with regex so I'm having hard time finding a solution.
I have a date/time in string format that looks like this:
"11/18/2022 12:00 AM"
I want to create another property that is just a day ahead (so "11/19/2022 12:00 AM"), so I need a regex expression that just points to the "18" in that string.
Any help or guidance is appreciated! Thanks.
I've tried this:
^(.)(.)(./[^/])$
which just replaces the whole string.

You could match the pattern with capture groups and use those groups in the replacement.
The value for 18 is in capture group 2, and the leading and trailing part in group 1 and group 3 in case you want to do a replacement.
^(\d{1,2}/)(\d{1,2})(/\d{4}\s\d{1,2}:\d{1,2}\s[AP]M)$
See a regex 101 demo
Note that when you want to increment the date with a day, it would be better to match the string and then use a programming language with a date function/api.

Related

ADF Dataflow - Replace spaces with underscore in column names

To remove spaces with underscore I am using replace($$,' ','_') expression in Select transformation
It works for a column "Period Key" and makes it "Period_Key" but for another column "Week in Month Description" it makes it "Week_in Month Description". So it is replacing only first occurrence
Can someone try this? Or how can we write regex for this?
I used below function and it worked
regexReplace($$,' ','_')

How to delete space in character text?

I wrote a code that automatically pulls time-related information from the system. As indicated in the table is fixed t247 Month names to 10 characters in length. But it is a bad image when showing on the report screen.
I print this way:
WRITE : 'Bugün', t_month_names-ltx, ' ayının'.
CONCATENATE gv_words-word '''nci günü' INTO date.
CONCATENATE date ',' INTO date.
CONCATENATE date gv_year INTO date SEPARATED BY space.
TRANSLATE date TO LOWER CASE.
I tried the CONDENSE t_month_names-ltx NO-GAPS. method to delete the spaces, but it was not enough.
After WRITE, I was able to write statically by setting the blank value:
WRITE : 'Bugün', t_month_names-ltx.
WRITE : 14 'ayının'.
CONCATENATE gv_words-word '''nci günü' INTO date.
CONCATENATE date ',' INTO date.
CONCATENATE date gv_year INTO date SEPARATED BY space.
TRANSLATE date TO LOWER CASE.
But this is not a correct use. How do I achieve this dynamically?
You could use a temporary field of type STRING:
DATA l_month TYPE STRING.
l_month = t_month_names-ltx.
WRITE : 'Bugün', l_month.
WRITE : 14 'ayının'.
CONCATENATE gv_words-word '''nci günü' INTO date.
CONCATENATE date ',' INTO date.
CONCATENATE date gv_year INTO date SEPARATED BY space.
TRANSLATE date TO LOWER CASE.
You can not delete trailing spaces from a TYPE C field, because it's of constant length. The unused length is always filled with spaces.
But after you assembled you string, you can use CONDENSE without NO-GAPS to remove any chains of more than one space within the string.
Add CONDENSE date. below the code you wrote and you should get the results you want.
Another option is to abandon CONCATENATE and use string templates (string literals within | symbols) for string assembly instead, which do not have the annoying habit of including trailing spaces of TYPE C fields:
DATA long_char TYPE C LENGTH 128.
long_char = 'long character field'.
WRITE |this is a { long_char } inserted without spaces|.
Output:
this is a long character field inserted without spaces

How to produce a formatted date string in Q/KDB?

How can one produce an ISO date string "yyyy-MM-dd" from a Q date type? I looked at concatenating the various parts but am not even able to get the day/month, e.g. d:2015.12.01;d.month prints 2015.12, i.e. more than just the month.
If you plan to do it on a large scale (i.e. a large vector/list of dates or a column in a table) and you're sure your dates are always well-formed, then you could use a dot-amend:
q)update .[;(::;4 7);:;"-"]string date from ([] date:2#.z.D)
date
------------
"2016-01-04"
"2016-01-04"
This way you wouldn't have to apply to "each" entry of the vector/list, it works on the vector/list itself.
q)"-" sv "." vs string[2015.12.01]
"2015-12-01"
vs vector from string, splits by "." above;
sv string to vector, join by "-" above.
Remember a string is just a char array, so you can grab each part as you require with indexing. But the above is useful as the resulting vector of vs gives a 3-length vector that you manipulate any way you like
I believe the shortest (and cleanest) option for ISO8601 UTC timestamp available since at least kdb v3.4 would be to use .h.iso8601 builtin
i.e.
q).h.iso8601 .z.p
"2020-11-09T15:42:19.292301000"
Or, if you just need milliseconds similar to what JS toISOString() does, use:
q).isotime:{(23#.h.iso8601 x),"Z"}
q).isotime[.z.p]
"2020-11-09T16:02:02.601Z"
q).isotime[2015.12.01]
"2015-12-01T00:00:00.000Z"
Note .z.p is important, as .h.iso8601 .z.P would silently give you local time without timezone (+0100 etc) so it would still be interpreted as UTC by compliant ISO8601 parser :(
Check-out this GitHub library for datetime formatting. It supports the excel way of formatting date and time. It might not be the right fit for formatting a large number of objects.
q).dtf.format["yyyy-mm-dd"; 2018.06.08T01:02:03.456]
"2018-06-08"
time formatting :
q).dtf.format["yyyy-mmmm-dd hh:uu AM/PM"; 2018.01.08T01:02:03.456]
"2018-January-08 01:02 AM"
I am using something like this:
q)ymd:{[x;s](4#d),s,(2#-5#d),s,-2#d:string[x]}
q)ymd[.z.D;"-"]
"2016-01-25"
q)ymd[.z.D;"/"]
"2016/01/25"
q)ymd[.z.D;""]
"20160125"
Or for tables:
q)t:([]a:5#1;5#.z.d)
q)update s:ymd[;"-"] each d from t
a d s
-------------------------
1 2016.01.26 "2016-01-26"
1 2016.01.26 "2016-01-26"
1 2016.01.26 "2016-01-26"
1 2016.01.26 "2016-01-26"
1 2016.01.26 "2016-01-26"
Please change the separator like - or / in the update statement.
update s:{ssr[string x;".";y]}'[d;"-"] from ([]a:5#1;5?.z.d)
a d s
-------------------------
1 2010.12.31 "2010-12-31"
1 2012.08.24 "2012-08-24"
1 2004.12.05 "2004-12-05"
1 2000.10.02 "2000-10-02"
1 2006.09.10 "2006-09-10"

Trouble determining the pattern for NSRegularExpression...?

i am relatively new to NSRegularExpression and just can't come up with a pattern to find a string within a string....
here is the string...
##$294#001#[12345-678[123-456-7#15665#2
I want to extract the string..
#001#[12345-678[123-456-7#
for more info I know that there will be 3 digits(like 001) between two # 's and 20 characters between the last two # 's..
I have tried n number of combinations but nothing seem to work. any help is appreciated.
How about something like this:
#[0-9]{3}#.{20}#
If you know that the 20 characters will always consist of digits, [ and -, your pattern would become:
#[0-9]{3}#[0-9\[\-]{20}#
Be careful with the backslashes: When you use create the pattern with a string literal (#"..."), you need to add an extra backslash before each backslash.
You can test NSRegularExpression patterns without recompiling each time by using RegexTester https://github.com/liyanage/regextester

How can I use regex to put quotes around a statement

I am using regexkitlite to validate some data in my iPhone app in xcode.
I am making an api call that sends a json result of:
"taskDate": newDate("September 23, 2011 00:00:00")
how do i use regex to convert it to:
"taskDate": "newDate("September 23, 2011 00:00:00")"
I want to surround the value of every "taskdate" key with quotes.
Edit: Adding OP's comment
Here is what i am using:
[resultString replaceOccurrencesOfRegex:#"new Date((.*?)\")," withString:#"\"\"," range:NSMakeRange(0, [resultString length])];
where resultString is the string containing the "new Date(...."
You can use regex to identify where in your text you have text in the format of "taskDate": "newDate("September 23, 2011 00:00:00")" but the actual replacement you will have to write yourself. Regex doesn't replace strings, it finds patterns within a string. Now, in order to find the pattern of "taskDate": newDate("<anything can go here>") you can use
"taskDate"\: newDate\(".*?"\)
If it is possible to have something else within the parenthesis, you will have to be more specific and only specify a date inside:
"taskDate"\: newDate\("[a-zA-z]* \d{2}, \d{4} \d{2}\:\d{2}\:\d{2}"\)
This will match everything of the sort of "taskDate"\: newDate\("Letters 00, 00:00:00"\). From here you can either make the months specific, and allow white space between all the quotations. All these changes makes the regex more complex, so only make it more strict to avoid matching things that you don't want. If no scenario exist that something else can be inside the parenthesis, I would go with the first regex.
That said, after you match the string within your content, you need to write the code to surround it with the quotations.