Posts by hkotsubo • 55,826 points
1,422 posts
-
2
votes3
answers2356
viewsA: Access data from a JSON structure
The other answers already gave the code that does what you need. But just to complement, I think it’s important you understand the format of a JSON, so you can work better with any data in the…
-
2
votes2
answers215
viewsA: Splitting a string into an array of strings from the occurrence of a date
For this we can use a regex that instead of using the date for the split, uses a comma, provided it is followed by "date, time - code". Assuming that the code is always numeric, a solution would be:…
-
8
votes1
answer8513
viewsA: How to do Localdatetime(Datetimezone.forID("America/Sao_paulo")) in Java.time
By code (DateTimeZone.forId), I’m assuming you’re using the Joda-Time. In the case, DateTimeZone.forID("America/Sao_Paulo") creates an object that contains all Timezone information…
-
3
votes3
answers497
viewsA: Convert string into a list where each element in the list is 3 characters from my initial string
The instruction i = i + 3 within the for does not work because this value of i is always overwritten by the next value of range. Example: for i in range(5): print(i) i = 100 This code prints the…
-
3
votes2
answers88
viewsA: incrementation table test
The difference is that ++x first increases the value of x, and only then use this value for whatever it is being used. Now x++ first uses the value of x in the expression, and only then increments.…
-
3
votes3
answers5171
viewsA: Remove string quotes in array
Just use replace and replace all quotes with an empty string (which is the same as removing them): print('Resultado ---> ', ''.join(lista).replace("'", "")) With this, the simple quotes (') are…
-
4
votes2
answers148
viewsA: Split into whois using regular expression
Complementing the answer from Andrei, follow some alternatives (considering the case where the whole string is in one line): let str = '2019-03-07T02:48:17-03:00<br /><br />domain:…
-
1
votes2
answers297
viewsA: Determine time in function
You can use the function date to get the current time. Just remember that this function - despite the name - returns a string. A another answer suggests to return the time in the format "hh:mm:ss"…
-
11
votes4
answers2085
viewsA: Does the date conversion always remain in en-BR?
As I said before here and here: Dates have no format A date is just a concept, an idea: it represents a specific point in the calendar. The date of "1 March 2019" represents this: the specific point…
-
1
votes1
answer322
viewsA: Date does not "respect" PHP formatting
The problem is in the first line (strtotime(date('d/m/Y H:i:s'));). According to the documentation of strtotime: Dates in the m/d/y or d-m-y formats are disambiguated by Looking at the separator…
-
2
votes3
answers1067
viewsA: Enter a number and display the next 100 even numbers
To william’s response already explains the probable cause of your program to display the result without you typing anything. I would just like to suggest an improvement in your algorithm. If you…
-
1
votes1
answer490
views -
2
votes1
answer174
viewsA: How to run find -exec on files that have been modified for more than 15 days?
First of all, -mtime 15 brings the files modified exactly 15 days ago. If you want the ones that were modified there is plus 15 days, use -mtime +15. As for the option -exec, lacked to put a \;:…
-
1
votes1
answer674
viewsA: zip argument #1 must support iteration (did not understand this error)
According to the documentation, zip takes several iterable objects (such as lists or tuples, for example) as parameters. In your case, you created vetorX = [5, 5, 5, 5, 20] and passed this list to…
-
0
votes2
answers61
viewsA: Calling an object of an array by the index in a loop
What you have is not a matrix, but an object: a set of pairs "key/value", keyed ({ and }). In this case, the keys are br, pr, etc. And the respective value of each of these keys is another object…
javascriptanswered hkotsubo 55,826 -
5
votes3
answers1422
viewsA: Clear string with Regex
You can use the function preg_replace to make the replacement. The regex can be something like ^\w+ (\d+ -[^\-]+)( -.*)?$: the markers ^ and $ are, respectively, the beginning and end of the string.…
-
2
votes2
answers602
viewsA: Get the date before an informed one
Heed, your code looks like be picking up the date of the previous day, but it was just luck/coincidence. Let’s understand what actually happens. Builder of Date When you pass a string to the builder…
-
10
votes2
answers804
viewsA: Regex for single quotes
You can use the regex (?<!^)'(?!$). The syntax (?<! determines a negative lookbehind, that is, it checks if something does not exist before the current position. Within this lookbehind we have…
-
2
votes1
answer1482
viewsA: Turn String into Localdatetime
A LocalDateTime needs the date and time to be created, but the String that you send ("25/07/1981") only has the date. An option to solve this in Java code would be to do the Parsing of this date and…
-
1
votes1
answer1326
viewsA: How to validate a field To accept only Letters/Numbers using Regex
The regex classes are in namespace System.Text.RegularExpressions. For your cases, I recommend using the markers ^ and $, meaning, respectively, the beginning and the end of the string. Thus you…
-
2
votes1
answer1776
viewsA: Regular expression for e-mail validation with REGEXP_LIKE on Oracle
First of all, you can simplify your expression a little. Instead of a-zA-Z0-9 it is possible to simply use [:alnum:]. And [.]{1,1} means "at least 1 and at most 1 endpoint" - meaning quantifier…
-
2
votes2
answers1288
viewsA: Replace last occurrence of "a" in a string
In fact, the regex you used (/a$/) only replaces the a in the end precisely because you used the bookmark $, meaning "end of string". In addition, the for is unnecessary as regex will already…
-
3
votes2
answers1248
viewsA: Expression to treat URL with parameter
I don’t know if I understand exactly all the rules and maybe I’m oversimplifying the problem, but come on: Parameter s For the parameter s, you want the end result to be the string ?s=, and wants to…
-
5
votes2
answers3202
viewsA: Regular expression for repeated characters
To answer from Andrei is much simpler, but since a solution with regular expressions has been requested, follow below: if (preg_match('/^(.)\1*$/', $str)) { // string tem todos os caracteres iguais…
-
1
votes1
answer403
viewsA: How to check dates in input datetime?
The first thing is to understand that the value of a field datetime-local is a string in format ISO 8601. Example: function validar(evt) { let dataHoraInicio =…
-
0
votes2
answers213
viewsA: Java JSP return of age
I don’t know if it was a typo posting, but look at this line: int idader = new Idade().calcularIdade(nascimento); ^ You declared the variable idader (with a r at the end) and then tried to use…
-
4
votes2
answers299
viewsA: Regex - Selecting the first occurrence of a sequence of a number block
Your regex took the last occurrence of the 6 digits because the quantifier * is greedy: he takes the largest possible sequence of characters satisfying the expression. And how you used .* and the…
-
4
votes3
answers1988
viewsA: Email validation function
About your regex: ^[\w-]+@ [a-z\d]+\.[\w]{3} I don’t know if it was a typo, but notice there’s a gap after the @. This makes the expression only validate emails that has a space there (like user@…
-
0
votes2
answers205
viewsA: How to pass command result to a bash variable?
Just use the syntax of Command Substitution, which can be done in two ways. You can put the command between `: resl=`cat frutas.txt | tail -n 1 | cut -d: -f 1` Or you can put the command between…
-
12
votes1
answer591
viewsA: Add list values with different sizes
You can use the method zip_longest module itertools. Just pass the two lists and set the value default to be used to fill in the missing values (in this case I will use zero). As in your lists the…
-
2
votes2
answers651
viewsA: Normalize or other way to format String
Before talking about your problem itself, a short introduction on Unicode: Normalization In Unicode, each character* has a unique numeric code (called code point, read this article to understand the…
-
5
votes2
answers154
viewsA: Sorted returns white space inside the Array
First understand that input returns a string. So if you type 3 2, that is a string (a text). Although there are two numbers in this string, they are not interpreted as actual numbers. And when you…
-
1
votes1
answer191
viewsA: Shell Script - "split" em for
First you can use tail to ignore the first line (the header). Using the parameter -n +X, being X the line from which you want to consider. Whereas the file is: -----id--------action H000tg6b JOB…
-
6
votes2
answers640
viewsA: How to allow white space in any part of the sequence in a regular expression?
Just put a space inside the brackets: /^(?=.*?[a-z])(?=(?:.*?\d){9})[a-z\d ]{12,}$/i ^ aqui Notice that there is a gap between the \d and the ]. The brackets define a character class, that is, they…
-
6
votes1
answer86
viewsA: Is there a shortcut to go to the last edited line?
On Windows and Linux, you can use ALT+- ("alt" + "hyphen"). In Mac OS the shortcut is CTRL+-. Typing several times in a row, it goes back in all positions that the cursor was, including on different…
-
3
votes1
answer150
viewsA: Loop Problem and Time Calculation
The problem is you’re using strftime, that converts a date/time into a string. But you want to do the opposite: given a string containing a time, convert it to a datetime. So in this case you should…
-
4
votes2
answers3235
viewsA: Format Localdate and pass as Parameter
If you "return date in format dd/MM/yyyy", it ceases to be a date (a LocalDate) and becomes a String. To better understand, below is a - long - explanation. Dates have no format A date is just a…
-
0
votes1
answer160
viewsA: Set an attribute of another class
If the getters and setters were implemented in the "traditional" way, I imagine they are like this: public class Produto{ ... // demais campos, setters, getters, etc public String descricao; public…
-
3
votes1
answer236
viewsA: Add objects of different types in the same vector
The problem is that Musica and Video are different classes that have no relation to each other. Therefore, when trying to place a Video in an array of Musica, gives build error. There are some…
-
2
votes2
answers104
viewsA: Set final variable inside a lambda gives error, but if it is not final, gives another error
The variable result was declared outside the lambda (in this case, outside the forEach), then it can only be used within it if it’s final. But a variable final may only have an assigned value only…
-
3
votes2
answers2073
viewsA: Find the largest and smallest element of a matrix
In your algorithm to check the highest value, you start with the first position of the matrix: resp = (0, 0) Then within the loops you make the comparison: if matriz[lin][col] >…
-
3
votes3
answers530
viewsA: Find an object in the array that has a specific key
You can use the method filter to bring only the elements that have the desired key: let specsFiltradas = specifications.filter(spec => spec.key == 'Valor'); This returns another array, containing…
-
1
votes1
answer79
viewsA: Date calculation error using struct tm
Probably the cause is the end of Brazilian daylight saving time: At midnight of the day 17/02/2019, the clocks are delayed by an hour, back to the 23h of the day 16. That is, when the clock is at…
-
20
votes2
answers944
viewsA: How do I find a character pattern corresponding to a date in a text?
To search for patterns in a text, a good solution is to use regular expressions. In Python, they are available in module re. To search for the numbers, you can use the shortcut \d (that takes any…
-
3
votes4
answers295
viewsA: Print null or non-null in Python array
No need to keep adding up the elements, just go through the lists and check if each value is different from zero. One way to solve is to first create a function that checks if a list has only zeros:…
-
11
votes1
answer962
viewsA: How to put parentheses or brackets in regular expressions in Javascript?
Just escape the character with \. Just remembering that inside a string, the character itself \ should be written as \\: let word = '\\['; let re = RegExp('\\b' + word + '\\b', 'g');…
-
1
votes2
answers949
viewsA: How to get the value of a field of a JSON object
First we have to understand what the structure of a JSON. Using your case as an example: { "DataInfo":[ { "Earnings": "0€", "Orders": "0" } ] } The keys ({ and }) define an object, which is nothing…
-
0
votes2
answers82
viewsA: Returning Added Data in a Dictionary
As explained by reply from @Rafael, you need to go through the entire list to print the data of all students. By doing lista[1], you are only accessing the second student (since arrays begin with…
-
2
votes1
answer1629
viewsA: Generate random characters with the Random class
The code is using the class java.util.Random, which is a native Java class for random number generation. The problem is that you have created a class with the same name (Random), and then the…
-
2
votes1
answer232
viewsA: Error: Collections.defaultdict' Object has in Python iteritems attribute
Are two problems: In the Python 3 no longer exists iteritems(), being replaced by items(). When calculating: sum(salario)/len(salario) You are using the variable salario, which was used a few lines…