Questions tagged [regex]
Regular expressions are a declarative language, mainly used for pattern matching within strings. Please include a tag specifying the programming language you are using, together with this tag.
865
questions
4
votes
1
answer
130
views
Python function to expand regex with ranges
I attempted to implement a python version of this function in my previous question. Given a regular expression with range(s) of alphabets/numbers, the function will expand the brackets and return a ...
1
vote
0
answers
49
views
R function to expand range of numbers/alphabets in a regex
I tried writing a short function that expands brackets [] within a regular expression. Given a regular expression, the function will expand the brackets and return ...
3
votes
1
answer
89
views
Build a dictionary from a string by the extraction of data from all the pairs <TAG|VAL> contained inside the string and clean string from TAGs
I have written code to manage a string and retrieve from it a dictionary which must contain pairs key-value dependent from the string.
To be more clear I'll show ...
2
votes
1
answer
55
views
Combining multiple regexps using the `|` operator and grouping the regexps by their flags
I've implemented a function that creates a multiple regular expressions predicate. The idea behind the predicate is combining regular expressions using the disjunction operator ...
1
vote
1
answer
128
views
Function, which replaces consecutive white-space with a single white-space
Task-description: Implement a function, which returns a string with all consecutive whites-spaces replaced as a single-space.
Example: "a[space][space][space]b[space][space][space]c" shall ...
2
votes
1
answer
46
views
Multiple regexp replace in string
I made a function that prompts me for values of variables (formatted %^{var1}) it found in a string and fills in said values.
...
9
votes
2
answers
968
views
Swift-function which counts the letters, numbers, spaces, special chars in a given string
Task description: Write a function, which counts the letters, spaces, number and special characters in string.
My implementation:
...
6
votes
1
answer
1k
views
Username Validation RegExp
Working through the freeCodeCamp JavaScript curriculum and I found this RegExp task surprisingly tricky (maybe I just don't use RegExp very much).
The parameters are:
Usernames can only use alpha-...
4
votes
4
answers
2k
views
Matching words from a text with a big list of keywords in Python
I implemented the following code to retrieve medication names from a given text (prescription_text in the code). It works by matching words in the text with a list ...
2
votes
2
answers
124
views
Algorithm to compare OAuth2 Rich Authorization Requests
Authorization Requests spec defines new authorization_details parameter. The authorization server have to somehow compare this parameter to decide whether client ...
1
vote
1
answer
48
views
Check for valid dates for all steps of the date
I want to check if a given string is a date of the form YYYY-mm-dd, including all the steps in it, i.e. YYYY-mm-dd, YYYY-mm and YYYY.
For example, all of the following dates are valid:
...
4
votes
4
answers
3k
views
A function that uses a regex to parse date and time and returns a datetime object
The code works, but I think can be simplified, I don't know which specific steps, but my version seems to me quite complicated
I build a function that takes a specific string format and returns a <...
0
votes
1
answer
78
views
Parsing shortcodes out of a string
I wrote this shortcode parsing and it runs in \$O(N^2)\$. Is there a way to better optimize this?
...
4
votes
2
answers
160
views
Sort todo.txt items by due date in Python 3
I am using the following format for my task management: https://github.com/todotxt/todo.txt
Do some stuff +uni due:2022-12-31
Write some paper +uni due:2023-01-10
...
2
votes
1
answer
151
views
Implementing Linux style file name wild cards on Windows 10 PowerShell
Implementing Linux style file name wild cards on Windows 10 PowerShell
While implementing an objected oriented wc on Windows 10 in visual studio C++ I implemented a recursive file search with a weak ...
1
vote
2
answers
305
views
Replacing banned words from a given text
The following algorithm is used to find a word from Blacklisted words in a string (called text) and replace these words with censored text, as shown in the example.
Example:
...
10
votes
4
answers
4k
views
Determine age from given birth date
So this is my first ever attempt so it's obviously bad. There has to be a better way to do this. A big thing I haven't implemented is trying to guarantee the structure of the input(i.e making sure ...
1
vote
2
answers
173
views
Parse list of UserPrincipal objects into custom class
I'm wondering how I could optimize this method that takes in a list of UserPrincipal objects, filters them by Regex and inserts the new class instance into a list. It takes 4 to 5 seconds for an input ...
3
votes
2
answers
232
views
Thread-safe cached compiled Regex
I have a use case where I want to keep a pre-compiled regex, cached, for performance reasons, but the regex pattern also needs to be updated infrequently. Instances of CachedRegexFilter class will be ...
1
vote
1
answer
5k
views
A python script to download video from a website
I wrote this script for gaining experience, this script takes in a website link (NSFW) and download the video on that site. It uses regular expressions to parse the website source code to extract urls ...
4
votes
2
answers
1k
views
Add a number to a file name if does not exist or increase the number if it exist
I'm currently working on a solution to add a number to a file name if the number does not exist or increase de number by one if the number exist.
For instance if you input "filename.jpeg" ...
0
votes
1
answer
599
views
Using a RegEx to create a UUID string
I'm writing a Discord bot that sometimes requires a Minecraft account UUID to be looked up so this is the function (and MWE) I have written to do this.
...
0
votes
1
answer
168
views
Password policy with regex and zxcvbn
This code snippet shows an implementation of a password policy check.
- The method composition uses regex to verify that the password meets composition criteria (at least one number, one special ...
2
votes
1
answer
321
views
Text splitter using Regular Expressions in Python
I have been provided a text splitter class that will take a text input and use re.sub to make replacements when matches are found and also splits sentences up and stores them in a list.
I had an idea ...
2
votes
1
answer
109
views
Newspaper Bill Calculator CLI with Python (1 of 3, Core)
Code is posted after explanation.
Due to the size of the project, this is being posted in three separate posts. This also ensures each post is more focused.
Post 2 of 3, CLI: Newspaper Bill ...
2
votes
2
answers
115
views
Regex and pandas to read forecast sky condition string
DataFrame methods to parse the sky condition from a terminal aerodrome forecast.
A line in a taf can report zero-eight cloud layers. Cloud layers are required in predominate lines, and optional in ...
3
votes
1
answer
51
views
Replace badly formatted questionnaires in a document using regex
I have rather badly formatted questionnaires (i.e. ordered lists) in a bunch of documents that I want to clean up and replace the current version with the cleaned-up version.
Example text
...
2
votes
1
answer
131
views
Regex pattern matching to generate pandas multi index
Mostly just looking for a review of my regex and implementation of capture groups. its something I've been working to improve.
The indexes have somewhat of a pattern to them of being....
...
5
votes
1
answer
1k
views
Breaking Bad name generator
I've created a script to print all periodic table symbolic permutations of a string. As seen in the opening credits of each episode of Breaking Bad:
© 2010-2022 AMC Networks Entertainment LLC.
...
2
votes
1
answer
248
views
Extract regular words from string but retain all other elements and record their type
This snippet processes every regular (\w+) word in a text and reinserts the processed version:
...
7
votes
2
answers
1k
views
Automate the Boring Stuff CH 7: password strength test
This exercise comes from Automate the Boring Stuff Ch 7. The assignment is to use regular expressions to test the strength of a password. Password must be >= 8 characters, contain at least one ...
1
vote
1
answer
218
views
Regex to capture possible US date entries
The Problem
I'm relying on plaintext entry to capture dates.
End users are urged to enter as MM-DD-YY, but it'll be great to account for as many possibilities.
US ...
3
votes
3
answers
355
views
Regex to extract portions of string about shipment
The Problem
I'll be extracting up to 4 parts of a String that come from user-input.
All 4 parts need to be in order i.e. (1-4) below in that order
The first capturing group is required and the last ...
1
vote
1
answer
1k
views
Replace Multiple Matches with different Values
I want to replace multiple matches of a Regex with different values from a map.
I have for example the following string #id#_#date#_#value#_additional_text.
I now ...
3
votes
1
answer
189
views
Remove unwanted characters at the end of a string
I want to make this code better , its works now, but i think it can be better , i have a hook and one property is onChange that get value and can setState and return that value for value of input , in ...
3
votes
2
answers
667
views
Replace function for C++
I'm learning regex for c++, and tried to make a simple function. It is very small but I'm satisfied, because this is the first time I managed to pass function as argument. This is a replace function. ...
0
votes
2
answers
118
views
Python function for finding if all substrings exist in a string in sequence
I want to find if all of a list of substrings exist in a string, in the correct order (with or without spaces between them), it seems to be a good use for regex.
I think I need to build a regex ...
7
votes
7
answers
3k
views
DnD Die in Java with regex
For my game I need to roll some values with a die. A die is formally described as:
nDs[+a]
n amount of die (optional, 1 of not set, zero must be set explicitly)
D ...
1
vote
1
answer
126
views
using pandas and numpy to parse dirty .csv
I'm relatively new to Python have been writing for a few months now. I've started a Pandas Numpy project that starts with parsing large and somewhat sloppy formatted textfile, its not exactly csv but ...
0
votes
2
answers
63
views
Test strings with a lot of regular expressions
I've a list of strings and a list of regular expressions. I need to categorize each string to get what type of string it is based on regex.
This is my code:
...
1
vote
1
answer
3k
views
Regex detect URL without http(s):// and www
I want to locate urls without protocols in the text, and then add the protocol before them. This means I don't want urls that begin with http(s):// or ...
8
votes
3
answers
1k
views
Filter out ambiguous bases from a DNA sequence
I have this function:
...
7
votes
1
answer
417
views
Stop cheating on home exams using python
Background
I am teaching a course at university level, and have lately been working on creating a home exam for my students. However, the institute has given clear instructions that collaboration is ...
2
votes
1
answer
319
views
Find if a dataset of words contains any words containing all vowels by using exactly 5 calls to str_detect
Take the words dataset from stringr. An R4DS exercise challenges the reader to check if there are any words in that dataset that ...
1
vote
1
answer
2k
views
simplifying regex expression for timestamp
I was wondering if it's possible to simplify this regex string (JS):
...
4
votes
1
answer
381
views
Regex to match several slightly different strings
I work with a charity that does a number of jobs, more than 100, less than 1000 a year. Jobs are identified with a year, and an up to 3 digit serial number, so ...
7
votes
5
answers
146
views
readable validation with python regex and simple logic
I have come with such a code for checking IP addresses. But would like to know how clean and good it is from 1 to 10. What I care about are readability and simplicity. Please give me any feedback.
...
2
votes
1
answer
60
views
String formatting
My requirement is to format the input values to a particular format as "##.###/##.###"
Example - My input value can have alphabets, alphanumeric, numeric
Expired -- > if alphabet, output ...
0
votes
1
answer
2k
views
RegEx sentence-splitting madness. Mad genius?
I inherited the following regex:
/[^.!?\s][^.!?\n]*(?:[.!?](?!['"]?\s|$)[^.!?]*)*[.!?]?['"]?(?=\s|$)/g
This regex is used to split sentences, and is ...
5
votes
2
answers
6k
views
Date Detection Regex in Python
I worked on a problem from Automate the Boring Stuff Chapter 7:
Write a regular expression that can detect dates in the DD/MM/YYYY
format. Assume that the days range from 01 to 31, the months range
...