Hot answers tagged

36 votes
Accepted

Regex to parse horizontal rules in Markdown

Congratulations, what you have here is a case of really really bad backtracking. Remember the StackOverflow outage in July 2016? Because it's explained quite nicely there. regex101 is so kind as to ...
Vogel612's user avatar
  • 25.2k
30 votes
Accepted

Regex to first match, then replace found matches

Regex.Matches returns a MatchCollection which contains Matches which captures the index and ...
ChrisWue's user avatar
  • 20.3k
29 votes
Accepted

Convert Persian and Arabic digits to English

Be nice to the maintenance programmer, even (especially?) if you expect it to be you. If you're mixing characters which are visually indistinguishable but don't need to be literal self-representations,...
Peter Taylor's user avatar
  • 24.1k
26 votes

Converting snake_case to PascalCase

More effective solution: str_replace('_', '', ucwords($key, '_')); From Gears library
Cosmologist's user avatar
24 votes
Accepted

Escape user input for use in JS regex

I'm not a fan of "sanitizing" data. There isn't a clear definition of what "sanitizing" means, other than that it takes untrustworthy input and somehow makes it valid. It might entail discarding the ...
200_success's user avatar
20 votes
Accepted

Regex to enforce password requirements

Split the regex into smaller parts to check each rule individually. Here's the code for JavaScript. The same simple logic can be used for Ruby or any other language. ...
Tushar's user avatar
  • 2,972
20 votes

Determine age from given birth date

datetime provides all the methods you need. ...
QuasiStellar's user avatar
  • 2,229
16 votes
Accepted

Calculating the 'binary gap' of a number

I think using Regex will be too much for this little task. A simple split by 1s should suffice. ...
Xiaoy312's user avatar
  • 3,136
15 votes
Accepted

Count the occurence of nucleobases in DNA string

Your code is fine and readable from a C/Java perspective. I don't think it's particularly Ruby-ic to use return statements. Just put ...
Barry's user avatar
  • 18.4k
15 votes

Multi-dimensional array sort and filter functions

Stop. Close Excel. Take a deep breath. Now go and get an actual Database. MS Access. MySQL is free and Open-Source. It doesn't really matter. What matters is that Excel is not the program to be ...
Kaz's user avatar
  • 8,790
15 votes
Accepted

Parse complex text files using Python

There are a few performance tricks we can apply here: add __slots__ to the class definition should help with memory and performance as well: ...
alecxe's user avatar
  • 17.2k
15 votes
Accepted

Parsing an email string

Consider taking advantage of existing features that could provide an additional layer of validation. mainly System.Net.Mail.MailAddress Also as mentioned in a ...
Nkosi's user avatar
  • 3,236
15 votes
Accepted

Average spam confidence

Not duplicating any of @Peilonrayz's code review points ... Stop reading entire files into memory when you can process the file line by line in one pass, and stop creating huge lists in memory which ...
AJNeufeld's user avatar
  • 33.4k
15 votes
Accepted

Function in Python to extract web Data

Use Requests for HTTP and let it deal with the encoding Do not bake the query into the URL string Use BeautifulSoup for HTML parsing Use typehints Nice and simple: ...
Reinderien's user avatar
  • 60.5k
14 votes
Accepted

Basic JavaScript template engine using replace, match and RegExp

RegEx [ ]* vs \s* [ ]* which is same as *(space ...
Tushar's user avatar
  • 2,972
14 votes
Accepted

Convert a string like 4h53m12s to a total number of seconds in JavaScript

getValue(str) is such a vague name for the function and its parameter, it could mean anything! Furthermore, "get" implies that this is a getter function that ...
200_success's user avatar
13 votes

Filter out ambiguous bases from a DNA sequence

Perhaps there aren't enough test cases here, because I don't see why you can't just use: ...
Kraigolas's user avatar
  • 917
13 votes
Accepted

Automate the Boring Stuff CH 7: password strength test

The current implementation is too complex. As soon as you get the results from the three findall() calls, you have essentially everything you need, but the function ...
FMc's user avatar
  • 12.7k
13 votes
Accepted

Swift-function which counts the letters, numbers, spaces, special chars in a given string

Returning an array What's your opinion about returning the results in an array? Why would you do that? Because it's an easy way to return the result? Because you don't know how else to do it? How ...
Simon Forsberg's user avatar
12 votes
Accepted

Cleaning up date strings in Python

For starter, congratulations, the code is clean and uses rather good constructs. I just have two nitpicks about the layout: I prefer to indent lists before the first row: ...
301_Moved_Permanently's user avatar
12 votes
Accepted

Elegant way to replace substring in a regex with optional groups in Python?

...
Reinderien's user avatar
  • 60.5k
12 votes

Average spam confidence

I think ask_file_name looks fine without using pathlib. The difference between the two comes down to LBYL vs EAFP. For the most ...
Peilonrayz's user avatar
  • 43k
12 votes

Javascript Syntax-Highlighter for Java

There's a glaring problem with this: no user input validation. A basic test revealed this: Ah, but that's not Java you say? True, but what if some poor sod has a stray HTML tag in their docstring? ...
Mast's user avatar
  • 13.3k
12 votes

DnD Die in Java with regex

Bug in the validation pattern The INPUT_VALIDATION_PATTERN doesn't work correctly, as it matches the invalid input "D"....
mindoverflow's user avatar
11 votes
Accepted

Using regex to replace strange characters

RegEx Improvements The regex can be shortened by using case-insensitive match with i flag. We can remove the characters which are added as both lowercase and ...
Tushar's user avatar
  • 2,972
11 votes
Accepted

Convert human readable string to TimeSpan

public static class TimeSpanConverter { public static TimeSpan Convert(string input) To people who use WPF a lot, the name hints at this class being a ...
Peter Taylor's user avatar
  • 24.1k
11 votes
Accepted

Convert camelCase and PascalCase to Title Case

I like your solution quite a bit. It's clear, easy to read and I don't see any bugs. There are many ways to condense the replace calls as you mention, but I think ...
ggorlen's user avatar
  • 3,337

Only top scored, non community-wiki answers of a minimum length are eligible