The Stack Overflow podcast is back! Listen to an interview with our new CEO.

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.

Filter by
Sorted by
Tagged with
5
votes
2answers
116 views

Splitting each word using longest substring that matches in a vocab/dictionary

This function takes in a string and a fixed list of vocab: splits a string into words by spaces for each word, check in the dictionary/vocab and find the longest matching substrings, if none matching ...
2
votes
1answer
47 views

LeetCode 65: Valid Number (C++)

Problem Validate if a given string can be interpreted as a decimal or scientific number. Examples: ...
4
votes
3answers
593 views

LeetCode 65: Valid Number (Python)

Problem Validate if a given string can be interpreted as a decimal or scientific number. Some examples: ...
16
votes
2answers
592 views

Using lots of regex substitutions to tokenize text

I authored a piece of code that was merged into the nltk codebase. It is full of regex substitutions: ...
-2
votes
0answers
18 views

balanced curly brackets check from regex in C# [closed]

I have the following sentence, "Please check for {input1} and {input2} and {input3}. Thank you." I have the following regex to extract everything in braces. ...
-4
votes
0answers
18 views

RegEx validation: not sure if anchors are okay [closed]

I need to grab specific fields from several csv data log streams using a PCRE RegEx. All the fields in the log files are on a single line. ...
4
votes
1answer
89 views

Chuck Norris Joke Teller

I've created a command line joke teller which makes Chuck Norris jokes after communicating with the Chuck Norris API. It also allows you to generate a random person by communicating to the names API ...
7
votes
3answers
194 views

Find website URLs that begin with http:// or https://

This is a problem from Automate the Boring Stuff from "Pattern Matching with Regular Expression". I am a beginner in Python and the purpose of this program is to match urls which start with ...
3
votes
1answer
110 views

Word frequency analysis: Python

I wrote a very rudimentary code that counts sentences and words in the arbitrary text. Code: ...
2
votes
1answer
385 views

Function to convert a title to a slug

I'm creating a function that converts a title, to a URL slug. I'm not all to familiar with regular expressions, but I've done my best. Can you see any problems or improvements with the following ...
2
votes
0answers
72 views

Sanitize input for a “robot on a grid” kata

I have the following piece of code which is FSI tested and works. However, I want to trim it as much as possible (not only the comments, which are there for my own future reference) and make it as ...
2
votes
2answers
63 views

Manipulate urls with regular expressions instead of the slip approach

Basically I have an url which looks like this structurally speaking: http://www.my-site.com/topic1/topic2/topic3/topic4/topic5 and I want to do 2 things to it: <...
10
votes
3answers
8k views

Calculating the 'binary gap' of a number

Inspired by this question and therefore this one I decided that clearly the best most readable way to solve this is with Regular Expressions. A binary gap within a positive integer N is any maximal ...
3
votes
1answer
5k views

Compress and decompress a string from a file

I need to compress some text, I'm wondering if my program could be made more efficient. The only things I could think of would be the 'import re' and turning the filename input and read part as a ...
3
votes
1answer
42 views

Validate URL with Regex and java.net.URL

I'm writing a URL validator. Firstly, it checks for special characters in the input. Secondly, it adds 'http://' and checks for validity. ...
4
votes
1answer
47 views

Extract, filter and match three letters from the given arguments and predict the name

Case 1: rank1_naming This function takes two arguments list_proteins_pattern_available best_match_protein_name Objective: Extract the three letter pattern from the both arguments. Match the ...
1
vote
1answer
44 views

Swift: extract string with regex and range

In Swift, I have a string like this http://mnc-hdqp.oss-cn-shanghai.aliyuncs.com/user%2Fheat%2Fdefault.jpg?Signature=2BI%2BauSvy&Expires=1568682491&OSSAccessKeyId=LTAIQ8Lif1HHVkXd Need to ...
2
votes
1answer
46 views

Function to convert “path” into regular expression

This function converts a string like: /{?lang(2-5):lowercase}/{page(2+):lowercase}/{article}-{id:integer}*.html* Into the following regular expression: ...
7
votes
4answers
324 views

Sort files in a given folders and provide as a list

I have two folders query and subject. The following function sorts alphabetically and provides the query and subject as a separate list. The file names are mixed of numbers and I found the sort ...
3
votes
1answer
51 views

Run an external program and extract a pattern match along with the result file

The script takes two input files of protein sequences and runs an external program (installed in linux/MacOS). The result provides a text output file example output.. Identity percentage is extracted ...
1
vote
2answers
60 views

Extract three letter pattern from a given list

The code takes a list of names and extracts the three letter pattern from the names. Then return the list of names as a value in the dictionary with key as a pattern. The list name has a fixed naming ...
6
votes
1answer
55 views

Extract strings from nested array in Perl

Extract the strings from nested arrays in Perl. It prints: a, b, c, d, E ...
3
votes
1answer
2k views

Parsing and evaluating cron expression

I wasn't happy with my last attempt to parse a cron expression so I simplified it and now I use regex instead. I'd like you to take a look at this solution whether I'm doing something terribly wrong. ...
6
votes
1answer
4k views

Named placeholders filling and extraction in Java

Here's utility class that is capable of filling strings with placeholders and extract values of placeholders from filled strings. Placeholders are enclosed in % ...
4
votes
1answer
73 views

Regex XML validator

I have written a Regex to validate XML/HTML, along with any attributes. It aims to: Match any XML-like text Not match any unclosed tags Adapt for any spacing, newlines, etc. Be as generous as ...
6
votes
5answers
262 views

Simple tokenizer v2 - reading all matching chars at once

I have rewritten my tokenizer according to most of the suggestions from the previous question here. API It now reads all chars as long as they match the pattern. I use three types of attributes to ...
4
votes
1answer
2k views

Regular Expression to Remove Comments

This was developed in response to a question on Stack Overflow, Removing comments using regex. What the OP needed was a regular expression to use in C# to remove comnents from lines of a file. All ...
4
votes
1answer
53 views

Sorting email address according to occurrences of each domain

My code: ...
1
vote
1answer
44 views

Script to transpose chords in a song sheet

I'd like to write a script that receives text input from the clipboard that will be from a song lyric sheet with chords. The goal is for the function to return the text to the clipboard after ...
0
votes
1answer
60 views

Extract HTML tags and its content from a string [closed]

Goal I want to extract HTML tags and its content from a string. The content (input) is queried from WordPress database. Sample data (input) I extract this dummy data from my WordPress database: ...
1
vote
1answer
67 views

Format the output of Color.ToString() to RGB values [closed]

The method that I wrote below functions properly; however, I believe that the code I wrote is not clean nor "good". I know the basics of regex but I do not know more advanced techniques as I am still ...
0
votes
0answers
39 views

How to filter URL from the array list based on the string name with regExp?

The solution found to filtering pathname with customising current normalizeTme() function. For sure it is not the ideal way of ...
2
votes
1answer
38 views

HTTP scraper efficiency with multiprocessing

I built this scraper for work that will take a csv list of firewalls from our network management system and scan a given list of HTTPS ports to see if the firewalls are accepting web requests on the ...
1
vote
1answer
42 views

Short function to remove unnecessary whitespace

I have a function consisting of one line of code: def trimString(string): """ Remove unnecessary whitespace. """ return re.sub('\s+', ' ', string).strip() ...
2
votes
1answer
109 views

Form validation function for React

I need to validate multiple input fields using React. I am using a simple if-else block for each input field but I would like to optimize my code and make it better. I feel there can be a better way ...
12
votes
0answers
218 views

“String Interpolation in {language}”, “VBA”

"This is {0} cool!", "freaking" I've always wanted an easy and intuitive way to inject variables into a string. So after about 10 variations, I finally came up with this function. How it works The ...
1
vote
0answers
29 views

git status parser using regular expressions in javascript

I'm looking for improvements of a code that filter the output of git status --ignored and git status --ignored -bs commands. I ...
3
votes
2answers
60 views

Checking the strength of a password using regexes

I'm a beginner in Python and I made a password checker. I'm following these rules: I have to use regex password must be at least 8 characters must have at least one lower case character must have at ...
2
votes
2answers
129 views

Python program to get the first word

Given a string the task it to find its first word with some rules: The string can have points and commas A word can start with a letter, a point or space One word can contain one apostrophe and it ...
2
votes
1answer
50 views

Beautifulsoup and lxml (xpath) too slow with respect to regex when parsing HTML

I agree that using regex to parse HTML is not a good way, in particular I am worried about their fragility with respect to change in the HTML. The problem is that any alternatives are really too slow....
0
votes
2answers
85 views

A regex pattern that matches all forms of integers and decimal numbers in Python

The question is pretty self-explanatory. I wrote a regex pattern which should (in theory) match all possible integers and decimal numbers. The pattern is as follows: ...
2
votes
1answer
161 views

Checking if an object meets certain criteria to amend to certain Entity Objects?

I am trying to check if a class has only numeric values in it and then return a bool, however, some instances there may be non numeric chars in an object that ...
4
votes
2answers
92 views

Regex Version of strip()

This problem is from Automate The Boring Stuff using Python - Chapter 7. Write a function that takes a string and does the same thing as the strip() string method. If no other arguments are ...
17
votes
5answers
4k views

Speeding up thousands of string parses

I have a mapping application which takes string arguments in the form of string arrays. I parse these and then perform an action on the map. One of these is ...
0
votes
0answers
28 views

WordPress hook to add URL-rewriting rules for articles with language variants

Please look at this working WordPress code: ...
4
votes
1answer
63 views

Python log entry search utility

I wrote a small utility for searching our web app log files for certain strings. The idea is that if the search string is found somewhere in the entry, the entire entry should be printed. The ...
2
votes
2answers
498 views

Regex version of strip() - Ch. 7 Automate the Boring Stuff

Here is a practice exercise — Regex version of strip() \$-\$ Write a function that takes a string and does the same thing as the ...
3
votes
2answers
64 views

Minimizing a file path

I have a simple program that "minimizes" a file path. Minimizing simply means replacing unnecessary file entries with entries that mean the same thing: ...
4
votes
2answers
73 views

RegEx version control

this is my first time on this stack exchange :) I created a little script to check for the latest version of specified API version available, but I am wondering if I am using the best way possible ...