Posts by hkotsubo • 55,826 points
1,422 posts
-
4
votes1
answer171
viewsA: Regex for split comparison operators
An alternative is: String formula = " 100 != (50 + 20 + 30) "; String[] partes = formula.split("!?=|[<>]=?"); for (String s : partes) { System.out.println(s); } The regex uses alternation (the…
-
5
votes7
answers87300
viewsA: How to validate with regex a string containing only letters, whitespace and accented letters?
Just one detail: in several of the other answers a character class containing the space (such as /^[a-záàâãéèêíïóôõöúçñ ]+$/i, for example - notice that there is a space inside the brackets). It’s…
-
2
votes2
answers613
views -
2
votes2
answers1070
viewsA: Regular expression for alphanumeric characters, hyphen, space or single quotes
An alternative ("simplistic" and "naive" - we’ll understand why) would be: String[] v = { "Jean-da Silva", "Carlos 2", "João d'lango", "J@ão", "Carlos*", "teste_1" }; Matcher matcher =…
-
3
votes3
answers1639
viewsA: Date format in an NF-e
This format is defined by ISO 8601 standard. Basically, it defines the order of the fields (always "year-month-day-hour-minute-second-offset"), beyond their values and sizes: year with at least 4…
-
8
votes2
answers191
viewsA: Typescript - Incorrect code output - Date
It’s a little confusing, but according to documentation of Date, the numerical value of the month is indexed to zero. That is, January is zero, February is 1, etc. Therefore, new Date(1997, 1, 22)…
-
10
votes1
answer4854
viewsA: Convert a str to datetime in Python
strftime converts a date (a datetime) to a string (which is the opposite of what you want to do). In case, you have the return of input, which is a string (and not a date), and is passing it to…
-
3
votes1
answer691
viewsA: Insert a Datetime into a date type input tag
According to the documentation, the value of a field datetime-local must be a string containing the date and time in the format "yyyyyy-mm-ddThh:mm:ss.sss" (with the second and second fractions…
-
4
votes2
answers1554
viewsA: Add hours as Moment.Js
First we need to understand two important concepts. Consider the two sentences below: I started working at 8 am today I worked for 8 hours straight In the first case, "8 hours" refers to timetable…
-
9
votes2
answers8284
viewsA: What does the REGEX shortcut mean?
The list of characters shortcut \s considers may vary from a language/API/tool/engine to another, and even according to the configurations available in each. In general, the \s always consider the…
-
1
votes2
answers629
viewsA: Split by upper case letters
It depends a lot on how the sentence you want to separate is. If there are no accented characters, just use what has already been suggested in the comments:…
-
5
votes2
answers516
viewsA: Array.push() always stacking the last foreach item
What happens is that with each iteration you are overwriting the id and name of the object line. The correct would be to create a new object at each iteration: let data = []; const columns = [{…
-
1
votes3
answers1652
viewsA: Convert a JSON to a Java list
First let’s see what your JSON structure looks like (with some explanatory comments): [ <-- aqui temos um array [ <-- dentro desse array, tem outro array // e dentro desse segundo array, temos…
-
3
votes1
answer68
viewsA: How do I save the output of a REGEX to a new file
According to the documentation, the return of the method match, when regex has the flag g, is a array with all occurrences found (in this case, an array of strings). So myReturn is an array, and…
-
2
votes2
answers161
viewsA: How to remove column fields from a regular expression select?
It depends. If you guarantee that the queries are always valid, and that everything will be on the same line, the simplest is: let query = "SELECT nome, sobrenome FROM table"; let m =…
-
3
votes1
answer1668
viewsA: Invert simply chained list
An alternative is to first invert the list by using this algorithm: public void inverter() { Nodo prev = null; Nodo next = null; Nodo current = inicio; while (current != null) { next =…
-
3
votes1
answer993
viewsA: Moment getting the value of the previous month, even after adding one month
Let’s go in pieces: Order of parameters in the method add I don’t know what version of Moment js. you are using, but the documentation of the method add says the following: Before version 2.8.0, the…
-
1
votes1
answer187
viewsA: Java Class Constructor Fix to Sonar
If contrato was marked with the annotation @NotNull, then it means that this field cannot be null. But in your builders, or you just arrow the id, or do not arrow any of the fields (in the default…
-
1
votes1
answer126
viewsA: Indexoutofboundsexception No Get(0) do Crawler jsoup
According to the Jsoup documentation, the method getElementsByAttribute takes the name of the attribute to be searched for. In the case of your HTML, you seem to be looking for this element: <h3…
-
3
votes1
answer89
viewsA: Error of calculating days interval between two dates using diff in PHP
When calculating the difference between two dates using DateTime::diff, the result is a DateInterval, and this class "breaks" the difference in several fields (years, months, days, etc). To better…
-
6
votes2
answers192
viewsA: Am I required to set the size of a Linkedlist in Java?
Just take a simple test: LinkedList<String> list = new LinkedList<>(); list.add(1, "cde"); This code launches a IndexOutOfBoundsException, and the reason is in documentation. The…
-
1
votes3
answers74
viewsA: Displaying message of no number read and accepting different types of input
If you want the input to accept both numbers on the same line and on different lines, one option is to loop this way: numeros = [] while True: valores = input().split(); if not valores: # lista…
-
3
votes2
answers138
viewsA: Read input data and write read lines to a file
Just add the line break in the string, by passing it to the method write: with open('contas', 'w') as arquivo: while True: a = input('\nO que quer adicionar?: ') arquivo.write(a + "\n") # <--…
-
1
votes1
answer120
viewsA: When fetching the attribute of a tag, it is bringing the value of another
For this case, there are two solutions: use regex (more complicated and less indicated), or a parser HTML (simplest and most suitable). Both are detailed below. Regex The full HTML was not put, but…
-
1
votes1
answer413
viewsA: Data processing in JSON format with Regex in Javascript
Like I said before in the comments, If the JSON is coming with quotes exactly that way, then it is not a valid JSON. In that case, it is best to correct in the backend, so that it manages the JSON…
-
2
votes1
answer176
viewsA: How to do a regex to take from bar (/) to _, ie I just want the digits before _
In the simplest case (there is only one occurrence of numbers in each string), you could do: $data = <<<DATA /dir/user/Desktop/zip/1_0.zip /dir/user/Desktop/zip/2_0.zip DATA; if…
-
4
votes1
answer147
viewsA: How to validate a regex rule for a user quote?
Just change to: function validNickname(str) { let pattern = /^@[A-Za-z0-9._]+$/; return pattern.test(str); } function getMention(str) { str.split(' ') .forEach(word => console.log(word,…
-
2
votes1
answer995
viewsA: Convert week of year, day of week and year to date
If you have the year and the number of the week, and want a date corresponding to this week, just use DateTime::setISODate: $ano = 2019; $semana = 32; $d = new DateTime(); $d->setISODate($ano,…
-
3
votes1
answer187
viewsA: How to print vector formatted in Shell?
The problem is that when creating the array, you separate the string into several elements (one containing the O, another containing 331, etc.), but the spaces are lost. When trying to print the…
-
4
votes1
answer296
viewsA: Implementation Doubt in Object Orientation
The class IntegerSet represents a set, so - as I understand it - the methods for union and intersection should have these signatures: public IntegerSet intersecction(IntegerSet outro) public…
-
1
votes2
answers2065
viewsA: Display the sum of each row of a randomly generated matrix
Since "matrix" is actually a list of lists, just scroll through it with a for simple to iterate through lines (no need to create a range for this, see more details below). And as each line is a list…
-
5
votes1
answer508
viewsA: How to know if there is an element in a List index without error?
The builder new ArrayList(100) is only setting the initial capacity to 100, but the list itself will have no element. Just note that the code below prints 0: System.out.println(new…
-
4
votes3
answers343
viewsA: Regular Expressions: Lazy quantifier function "?"
Complementing the other answers, the ? as an indicator of lazyness is not limited to being used only with * and +. It can be used with any other quantifier, like the {}, for example. let s =…
-
1
votes1
answer57
viewsA: Value passed in Pattern tag keys for class name
According to the documentation, the number that goes between the brackets is the size (length) that the class name will have, but there are some details as to its value and the final result. When…
-
1
votes1
answer343
viewsA: Calculator doing operations on the result of a previous operation
If you want the calculator to accept expressions like "+10" that add 10 to the current result, then the Calculadora should store the current output internally, and the executed operations should…
-
7
votes2
answers83
viewsA: Is it possible to pass an array per parameter without instantiating it?
If you have a constructor that receives a list, there is no way to call it without having a list (unless you pass null, of course, but I don’t think that’s the point). So the answer to "It is…
-
3
votes2
answers9963
viewsA: Invert the words of a sentence, keeping their order in the sentence
This is because the entire content of frase is reversed, since the Slice [::-1] operates over the entire string. If you want each word of the phrase to be reversed individually, you need to separate…
-
3
votes1
answer61
viewsA: return a list of numbers that are in sequence
The problem is that if lista[i] == lista[i+1] compares whether the element is equal to the next. And since the list has no repeated elements, the result will always be empty (as it will never enter…
-
4
votes2
answers87
viewsA: Change the type of a variable in a statement of a line, change the type of variables that come and sequence, why?
Complementing the answer from Ricardo, the fact that variables are declared on the same line makes no difference. If so, it gives the same result: let num1 = "10"; let num2 = 5; let num3 = 10; let…
javascriptanswered hkotsubo 55,826 -
4
votes2
answers4974
viewsA: Print larger number of an array
One way to find the largest number is simply to go through the array and compare its values: int maior = Integer.MIN_VALUE; int indiceMaior = -1; for (int i = 0; i < grade.length; i++) { if…
-
3
votes2
answers2472
viewsA: Fetch words between quotation marks through regular expression
Just complementing, an alternative is: $string1 = 'string(10) "CURITIBA" string(11) "SP"'; preg_match_all('/"([^"]+)"/', $string1, $matches); foreach($matches[1] as $m) { echo $m.PHP_EOL; } The…
-
3
votes4
answers2074
viewsA: How to get the format in hours when it exceeds 24?
First we need to understand two important concepts. Consider the two sentences below: the film will be shown at two o'clock in the morning the film is two hours long In the first case, "two hours"…
-
6
votes3
answers502
viewsA: Add an hour to the current time
An alternative is to use strtotime, that accepts some special formats, among them +x hours to add hours: $data_pagamento = date('Y-m-d H:i:s' , strtotime('+1 hours')); In case, the string could be…
-
1
votes1
answer270
viewsA: How to iterate with Python 3 dictionaries?
Let’s go in pieces. First read the file de_para, and store each line (except the first one) in a list (using with to open the file, because it ensures that the file is closed at the end of the…
-
1
votes2
answers65
viewsA: length() giving Nullpointerexception
An alternative is to change the way you read the data: String entrada; int contador = 0; while ((entrada = input.readLine()) != null) { // enquanto conseguir ler alguma coisa if (entrada.length() ==…
-
6
votes4
answers319
viewsA: Regex - set 2 limits and pick up all content inside
Just by supplementing the other answers, the best option for your specific case would in fact be the first regex of the Reply by @Jefferson Quesado: ENERGIA ELETRICA CONSUMO[^%]*% She wears a…
-
3
votes1
answer166
viewsA: Can I use a combination of different algorithms to generate a password hash?
To $senhacodificada that you generated will always have 128 characters, which is a size that any modern database will support smoothly. In fact the limit of the main databases is much higher than…
-
1
votes1
answer133
viewsA: Read data from a file and save to a Set, but Set is null
There are several problems in the code, among them: you try to read the file twice (one in the method contarLinhas and another in the method customerId). The problem is that the first time already…
-
0
votes2
answers1900
viewsA: Calculate the number of the week within the month
Based on the its results, I’d say you’re not exactly calculating "the number of the week within the month". Proof of this are the results for 1 and 2 January 2000, which resulted in 5. How these…
-
3
votes2
answers637
viewsA: Regular word start expression until next number
At first, regex may not be the most suitable tool, because what you probably need is a parser of logical expressions (or something like that). Just see how complicated regex can become, depending on…