Take out string from "<string>" in Lua - string

I want to parse some data from a string in Lua. I have tried several combination of string.match and string.sub but no luck. Here is the detail...
str='[{id:78749,name:Hrithik Roshan,character:Vijay Deenanath Chauhan,order:0,cast_id:1,profile_path:/1uGhDRNCA9I4WvyD9TfgKYnLEhZ.jpg},{id:77234,name:Priyanka Chopra,character:Kaali Gawde,ord'
fixstr = string.gsub(str,"name:","<actors>")
fixstr = string.gsub(fixstr,"character:","<actors>")
print(fixstr)
fixstr1 = string.match( fixstr, "<actors>(.+)<actors>")
print(fixstr1)
Output:
Output of rint(fixstr)
[{id:78749,<actors>Hrithik Roshan,<actors>Vijay Deenanath Chauhan,order:0,cast_id:1,profile_path:/1uGhDRNCA9I4WvyD9TfgKYnLEhZ.jpg},{id:77234,<actors>Priyanka Chopra,<actors>Kaali Gawde,ord
Output of print(fixstr1)
Hrithik Roshan,<actors>Vijay Deenanath Chauhan,order:0,cast_id:1,profile_path:/1uGhDRNCA9I4WvyD9TfgKYnLEhZ.jpg},{id:77234,<actors>Priyanka Chopra,
What I am trying to do is get all the string between <actors>...<actors>
but it didn't worked. Can anybody help on this?

To get all the strings between <actors> and <actors>, use string.gmatch for global match:
for name in string.gmatch(fixstr, "<actors>(.-)<actors>") do
print(name)
end
Note the use of .- in which - matches zero or more occurrences, but it's non-greedy.
Output:
Hrithik Roshan,
Priyanka Chopra,
Actually, unless you need fixstr for other uses, you don't need to substitute name: and character: to <actors>, just one run of string.gmatch can do the job:
for name in string.gmatch(str, "name:(.-)character:") do
print(name)
end

Related

Increment Numbers in an Unknown Substring by One

I need to get the name of the last file that was copied and then increment by one to get the next file. The files follow the pattern DDMM###.TXT where ### will start with 000 and end with 005 for the day.
I know I can use substring to get the ###, and then increment by 1, and then used replace to replace the ### with the new incremented "string". I was just wondering if there was a more elegant way to do this.
$value = $filetotransfer.substring(4,3)
$newvalue = ([int]$value+1).ToString('000')
$filetotransfer = $filetotransfer.Replace($value,$newvalue)
Where $filetotransfer can be mmdd000.txt to mmdd005.txt depending on the time of the day.
If you're using PowerShell (Core) 7+:
# -> '0506001.txt'
'0506000.txt' -replace '(?<=^\d{4})...', { '{0:000}' -f (1 + $_.Value) }
In Windows PowerShell, the -replace operator doesn't support script blocks ({ ... }) as the replacement operand, which makes the solution more complex, because a direct call to the underlying .NET API is required:
# -> '0506001.txt'
[regex]::Replace('0506000.txt', '(?<=^\d{4})...', { param($m) '{0:000}' -f (1 + $m.Value) })
For an explanation of these techniques, see this answer.

System.JSONException: Unexpected character ('i' (code 105)): was expecting comma to separate OBJECT entries at [line:1, column:18]

#SalesforceChallenge
I'm trying to escape a string but I had no success so far.
This is the response body I'm getting:
{"text":"this \"is something\" I wrote"}
Please note that there are 2 backslashes to escape the double quotes char. (This is a sample. Actually I have a big to escape with lots of "text" elements.)
When I try to deserialize it I get the following error:
System.JSONException: Unexpected character ('i' (code 105)): was expecting comma to separate OBJECT entries at [line:1, column:18]
I've tried to escape by using:
String my = '{"text":"this \"is something\" I wrote"}';
System.debug('test 0: ' + my);
System.debug('test 1: ' + my.replace('\"', '-'));
System.debug('test 2: ' + my.replace('\\"', '-'));
System.debug('test 3: ' + my.replace('\\\"', '-'));
System.debug('test 4: ' + my.replace('\\\\"', '-'));
--- Results:
[22]|DEBUG|test 0: {"text":"this "is something" I wrote"}
[23]|DEBUG|test 1: {-text-:-this -is something- I wrote-}
[23]|DEBUG|test 1: {-text-:-this -is something- I wrote-}
[24]|DEBUG|test 2: {"text":"this "is something" I wrote"}
[25]|DEBUG|test 3: {"text":"this "is something" I wrote"}
[26]|DEBUG|test 4: {"text":"this "is something" I wrote"}
--- What I need as result:
{"text":"this -is something- I wrote"}
Please, does someone has any fix to share?
Thanks a lot.
This is the problem with your test runs in Anonymous Apex:
String my = '{"text":"this \"is something\" I wrote"}';
Because \ is an escape character, you need two backslashes in an Apex string literal to produce a backslash in the actual output:
String my = '{"text":"this \\"is something\\" I wrote"}';
Since Apex quotes strings with ', you don't have to escape the quotes for Apex; you're escaping them for the JSON parser.
The same principle applies to the strings you're trying to use to do replacements: you must escape the \ for Apex.
All that said, it's unclear why you are trying to manually alter this string. The payload
{"text":"this \"is something\" I wrote"}
is valid JSON. In general, you should not perform string replacement on inbound JSON structures in Apex unless you're attempting to compensate for a payload that contains an Apex reserved word as a key so that you can use typed deserialization.

string.Contains function is not working as expected in my golang code. Though first string contains the second string, condition fails

I am checking some shell command output in golang. I need to check whether the output contains my input string.
//myinput is a valid string
stdoutput2,stderr2,err2 := Shellout("some valid shell command | grep "+myinput)
if(err2!=nil){
glg.Error("Not able to retrive information from system")
glg.Error(err2)
return false
} else if strings.Contains(strings.TrimSpace(stdoutput2),myinput){
glg.Info("able to retrive information from system")
return true
} else {
glg.Error("Not able to retrive information from system")
glg.Error("stdoutput2 after trimming is-->"+strings.TrimSpace(stdoutput2))
glg.Error("myinput is :"+myinput)
glg.Error("std error is :"+stderr2)
return false
}
Here though stdoutput2 contains the value of myinput, my else if condition is failing.
The final output is
Not able to retrive information from system.
stdoutput2 after trimming is--> Name = customcsi-b35f3ea733
myinput is :customcsi-b35f3ea733
Please note the space between Name and = symbol. That is part of output.
Please help in resolving this. My golang version is 1.13.5
Any help would be much appreciated.
Do you have a newline at the end of myinput?
You could try
stdoutput2, stderr2, err2 := Shellout("some valid shell command | grep " + myinput)
myinput = strings.TrimSpace(myinput)

How to make the translations work with the Python 3 "format" built-in method in Odoo?

Since Python v3, format is the primary API method to make variable substitutions and value formatting. However, Odoo is still using the Python 2 approach with the %s wildcard.
message = _('Scheduled meeting with %s') % invitee.name
# VS
message = 'Scheduled meeting with {}'.format(invitee.name) # this is not translated
I have seen some parts of the Odoo code where they have used some workaround, isolating strings.
exc = MissingError(
_("Record does not exist or has been deleted.")
+ '\n\n({} {}, {} {})'.format(_('Records:'), (self - existing).ids[:6], _('User:'), self._uid)
)
But, does anybody know if there is a more convenient way to use the format method and make it work with translations?
_ return a string, so you can call format on it directly.
_("{} Sequence").format(sec.code)
or like this:
_("{code} Sequence").format(code=invitee.code)
when you export the translation in PO file you should see this for the second example:
# for FR translation you should not translate the special format expression
msgid "{code} Sequence"
msgstr "{code} Séquence"
and this for the first example:
msgid "{} Sequence"
msgstr "{} Séquence"
If you don't see this then Odoo must be checking that _() must not be followed by . so you can work around this by doing this for example by surrounding the expression by parentheses :
# I think + "" is not needed
( _("{} Séquence") + "").format(sec.code)
Because In python "this {}".format("expression") is the same as this ("this {}").format("expression")

search string format in current or next line until match

ics file generated by evolution:
BEGIN:VCALENDAR
CALSCALE:GREGORIAN
PRODID:-//Ximian//NONSGML Evolution Calendar//EN
VERSION:2.0
X-EVOLUTION-DATA-REVISION:2017-10-28T04:50:31.240215Z(0)
BEGIN:VTIMEZONE
TZID:/freeassociation.sourceforge.net/Asia/Kolkata
X-LIC-LOCATION:Asia/Kolkata
BEGIN:STANDARD
TZNAME:IST
DTSTART:19701014T230000
TZOFFSETFROM:+0530
TZOFFSETTO:+0530
END:STANDARD
END:VTIMEZONE
BEGIN:VTODO
UID:f13168013f7c8b9abc36c31e43028c34a3f40823
DTSTAMP:20171019T025556Z
SUMMARY:Trial
DTSTART;TZID=/freeassociation.sourceforge.net/Asia/Kolkata:
20171019T000000
DUE;TZID=/freeassociation.sourceforge.net/Asia/Kolkata:20171030T000000
PERCENT-COMPLETE:0
CLASS:PUBLIC
DESCRIPTION:Hello
SEQUENCE:2
CREATED:20171019T031330Z
LAST-MODIFIED:20171028T045031Z
CATEGORIES:Work
END:VTODO
END:VCALENDAR
The problem is in the line 20-23 where:
DTSTART;TZID=/freeassociation.sourceforge.net/Asia/Kolkata:
20171019T000000
DUE;TZID=/freeassociation.sourceforge.net/Asia/Kolkata:20171030T000000
So, the DTSTART's time is in the next line while the DUE's timestamp is in the same line. Hence, a simple python function like:
if line.startswith("DTSTART;TZID"):
line = next(finp)
dt = line.strip()[0:8]
wont work for both. This one won't works for DUE, and I have to make another function without next for that purpose.
How can I handle any such date/time etc irrespective of the linebreak?
Maybe using regex would be useful in your case,
try this:
import re
if line.startswith("DTSTART;TZID"):
line = next(finp)
dt = re.search('(.*)T.*', line).groups(1)
if line.startswith("DTSTART;TZID"):
dt = re.search('.*\:(.*)T.*', line).groups(1)
The first group that's wrapped in () is in both cases the datestamp.
To decipher the regex; .* matches anything until the explictly named character occurs, which is in the first scenario 'T' because of the newline and the "plain" string given to us. In the second the stamp is between ':' and 'T' which enclose the date.
Using String-Slicing could become difficult when you are not sure if the city is always Kolkata etc.

Resources