Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

Questions tagged [regular-expressions]

According to Wikipedia, a regular expression (abbreviated regex or regexp) is a sequence of characters that forms a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. "find and replace"-like operations.

Filter by
Sorted by
Tagged with
-1 votes
2 answers
113 views

What would be the performant approach to parsing a _limited subset_ of Markdown? [closed]

There are plenty of articles about parsing full Markdown/CommonMark/etc, but I'm interested only in a specific limited subset, so I'm wondering if a better approach is available to me, and would much ...
user avatar
31 votes
6 answers
11k views

Is exposing regex in error response to end user bad practice?

Say I have a have request payload PUT /user { email: "invalid" ... } In the backend there is a email regex, which I cannot modify. Currently the behavior is to output: { "error&...
user avatar
2 votes
2 answers
950 views

Alternative to using regex in Python

Background I do programming with Python and now and then i run into a situation where i have to use regex Typically i try to learn a bit about it and look at examples of doing things similar to what i'...
user avatar
  • 409
0 votes
0 answers
123 views

Domain specific language for regex processing

I am working on a DSL for text processing. The core is searching for regular expressions with some operators around them. These searches are embedded into a more procedural program, which allows for ...
user avatar
  • 237
-5 votes
1 answer
428 views

In what programming language is Python's regex module written in? [closed]

If I try to rewrite specific regex functionalities (e.g. substituting a string) in Python, a solution using the regex module is always faster. Is regex written in C?
user avatar
  • 93
2 votes
3 answers
757 views

What is the right algorithm to match regex on a stream?

I'd like to match a regex pattern on a stream, but I am not sure what algorithm to use. I certainly don't want to load the entire file into memory. I tried to figure out how to do this, but I have ...
user avatar
  • 1,141
-4 votes
1 answer
3k views

What is the best use case of a regular expression (TripleByte question)?

I have lots of experience with regexes. I just want to make sense of this. Am I missing something here. Which one of these would you rather validate with a regex? Question reads, Which of the ...
user avatar
0 votes
1 answer
44 views

portable hashable string representation of data object

I have a class of (flat) objects that are going to be passed around between three different parties, each possibly running a different software stack. my_object: item: "string_a" money: ...
user avatar
1 vote
1 answer
145 views

Is it possible to create a regular language from an non regular language?

I am wondering if it is possible to create a regular language from a irregular language if we add or remove finite number of words from it? say L is irregular, can we add or remove finite number of ...
user avatar
2 votes
4 answers
734 views

What is the optimal way to perform 5000 unique string replace functions in terms of performance?

Restructuring some code, and the way I built it up over time has portions that look something like this: s.replace("ABW"," Aruba "); s.replace("AFG"," Afghanistan "); s.replace("AGO"," Angola "); s....
user avatar
  • 3,319
1 vote
1 answer
3k views

Is it a good practice to include empty/white space checks in RegEx?

The goal of the RegEx is to match exactly 6 characters, but in addition it should match empty strings or white space (e.g: ^$|\s|^(\w){6}$). Is it good practice check for empty string/white space in a ...
user avatar
  • 157
2 votes
1 answer
169 views

Why are `]` and `}` not metacharacters in Regex when ')' is?

In Regular Expressions Quick Start, it reads Twelve characters have special meanings in regular expressions: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar ...
user avatar
9 votes
4 answers
444 views

Guarding user input of regular expressions against attacks

I am aware of Regular Expression Denial of Service (ReDoS). Is there any reasonable way to allow users to create custom regexes while guaranteeing that they don't submit an exponentially slow pattern?
user avatar
  • 429
4 votes
1 answer
797 views

How to parse a simple custom syntax in Go?

I have a limited amount of input types: 34:56 = sensorA#, sensorA#, sensorB# 2:5 = { led# } 66 = otherSensor 2,3,4,5 = greenRelay#, redRelay#, relayA#, relayA# a:b implies range. {name} implies a ...
user avatar
5 votes
2 answers
2k views

Is the negation of a regular expression still regular?

All articles (example) I read till now about regular expressions and NFAs explain three operations: sequence alternation (union) repetition (Kleene star) No one talks about negation. Is the ...
user avatar
  • 343
10 votes
5 answers
759 views

Is placing text markers inside of strings bad style? Is there an alternative?

I work with massive strings which need a lot of manipulation. For example, I might generate a string like this: Part 1 Boat Section A Programming Part 2 Partitioning boats for ...
user avatar
  • 3,319
-2 votes
1 answer
1k views

What string will match the regular expression (^$)|(^.*\\{count\\}.*$)\n?

I'm trying to pass a parameter to create a thumbnail in AWS Elastic transcoder, which shouldn't be that difficult except I know nothing about regular expressions. My log is giving me the following ...
user avatar
  • 11
3 votes
2 answers
6k views

Is it appropite for a tokenizer to use regex to gather tokens?

I have recently caught the 'Toy Language' bug, and have been experimenting with various simple tokenizer configurations. The most recent one, makes use of the boost.regex library to identify and get ...
user avatar
1 vote
2 answers
12k views

Mapping enum values into regexes

I'm doing some code cleanup and I'm looking at my regexes. I have an extremely simple one: (ARA|CHI|FRE|GER|ITA|JPN|RUS|SPA)\s[0-9]{3}-[0-9]{2} It basically validates course identifiers for a ...
user avatar
7 votes
2 answers
255 views

Finding and counting equal substrings in a set of strings

I'm thinking about a way of finding similar parts in Strings. I have a set of strings of varying length i.e: The quick brown fox jumps fox force five the bunny is much quicker than the fox is First, ...
user avatar
  • 207
0 votes
1 answer
84 views

Is PCRE execution time primitive recursive or recursive?

In other words, is it possible to create a PCRE "regular" expression which takes Ackermann(m,m) time for an m length string?
user avatar
  • 363
0 votes
4 answers
798 views

How to escape regexes and paths in double quote strings without going crazy?

It often happens that after designing my regexp (on regex101.com) I want to paste it in my program. Consider this regexp that matches numbers and string (but keep in mind this is general question!): ^...
user avatar
0 votes
1 answer
278 views

Why is the "global" RegEx flag in JavaScript considered a property of regular expression itself?

In JavaScript, RegEx objects can be instantiated with flags such as g for global matching and i for case-insensitive. Intuitively, it makes sense for i to be a property of the constructed RegEx, ...
user avatar
  • 103
35 votes
6 answers
5k views

Should there be unit tests for complex regular expressions?

Should I write unit tests for complex regular expressions in my application? On the one hand: they are easy to test because input and output format is often simple and well-defined, and they can ...
user avatar
  • 434
0 votes
1 answer
68 views

Creating hyperlinks in documents and messages

In Sweden the abbreviation "t.ex." means "for example" ("till exempel") and it is extremely common. Every time I write that t.ex. in an email or in a text message it gets marked up as a clickable ...
user avatar
7 votes
2 answers
5k views

Guidelines for returning None vs. raising Error for Python string-validating functions

Let's say we have a function that checks if a given pattern occurs in a string. The default behaviour is that if the pattern matches, then it returns the pattern. So far so good but what is the ...
user avatar
  • 171
-1 votes
1 answer
312 views

Enable matching of character sequence made up of regex control character [closed]

Java Pattern provides flags that enables you match regex control characters. Passing Pattern.LITERAL or 16 as second argument will match any Unicode char, but not if you use a word bound with the ...
user avatar
-2 votes
2 answers
243 views

How to find all occurrences of a variable within a source code?

The title is pretty self-explanatory. I have a minified JavaScript code (100k+ lines) and would like to find all occurrences of a particular variable so I could rename it to better understand code. Is ...
user avatar
  • 11
3 votes
1 answer
2k views

Is it possible to write a regex that does one search then uses its results to do another search?

I'm searching for strings matching the pattern [A-Z]\W*[0-9]+, so that in V-2345 35A235 Q252 the V-2345 and Q252 would match. In another list, I want to find equivalent items that fit the same ...
user avatar
  • 363
1 vote
0 answers
49 views

Correlating Search Classifiers in a Database Scan for Sensitive Information

Problem Description I am working on an enterprise data discovery project that is designed to scan databases for sensitive information. The basic search unit is called a classifier and covers things ...
user avatar
1 vote
1 answer
404 views

What would be an actual DFA/actual regex engine solve this simple regex pattern?

.*foo Academically, that is a regular expression. A string aaaaaaaaaaaaafoo will match that. Also string aaaaaaaaaaaaafooaaaaafoo will match that too. I would imagine that an NFA that solve that ...
user avatar
  • 709
167 votes
10 answers
14k views

Is there a specific reason for the poor readability of regular expression syntax design?

Programmers all seem to agree that readability of code is far more important than short-syntaxed one-liners which work, but require a senior developer to interpret with any degree of accuracy - but ...
user avatar
  • 3,843
2 votes
1 answer
183 views

Regex backtracking on simple pattern

I've got this code in Mathematica that fetches the left or right side of an equation by turning it into pure text then textually getting rid of the other side through regex. I've isolated the faulty ...
user avatar
  • 159
4 votes
2 answers
2k views

why regex, when using global search and {0,} quantifier, match the end of the string?

I have asked a question here about js, regex, quantifiers and global search. I've understood finally how this works, but, let's take a concrete example and then I`ll write my question. Based on the ...
user avatar
0 votes
1 answer
142 views

Is the regex syntax in .net 3.5 the same as the regex syntax in the latest version of .net framework in c#?

Is the regex syntax in .net 3.5 the same as the regex syntax in the latest version of .net framework in c#?
user avatar
  • 111
1 vote
1 answer
233 views

Matching groups of similar lines on a generic matching algorithm

I have to write a program to search through a file containing lines and find lines that match to a degree of tolerance but are not necessarily the same. So for example the following lines would match: ...
user avatar
3 votes
1 answer
244 views

Features of a routing system

I have been tinkering with the idea of creating a routing system as a learning experience and not for some production purpose. I have trying to decide what features are a must have and what features ...
user avatar
0 votes
1 answer
378 views

How to choose between different ways to write some code?

I would like to validate point in a two-dimensional rectangular Cartesian coordinate. I found following regular expression solution on stackoverflow ^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$ . Another ...
user avatar
1 vote
0 answers
207 views

Simplifying backreferences

This is a theoretical example for simplicity's sake. I use flat values in the regex to make it easily legible for almost anyone, since lots of {,(,[,\'s all jammed together can be a headache to read. ...
user avatar
1 vote
1 answer
672 views

Understanding of concatenation, union and iteration at regular language

I'm trying to understand what a regular language is. There are 3 compound regular expressions for regular language: concat {AB}, union {A+B} and iteration {A*}. When they are used in a simple ways it'...
user avatar
2 votes
3 answers
5k views

Can regexp be used to check for palindromes?

Can regexp pattern matching be used to check for palindromes within a given text in Java? My intuition is that in order to check for palindromes, we may need to remember the character that was parsed,...
user avatar
1 vote
2 answers
426 views

Writing a simple code validator

I know that programming languages can be defined in EBNF which can be converted into regular expressions. Right now I am working on a very simple BASIC interpreter for a project. The code has to be ...
user avatar
  • 133
3 votes
1 answer
3k views

How to create an algorithm for determining Boolean Tautologies

So i am right now exploring some topics in a proof course and it occurred to me to try to create a boolean tautology solver. I would like an algorithm that is more efficient than brute force. Problem ...
user avatar
5 votes
2 answers
336 views

Need overview of concepts and tools to translate a DSL to regular expressions

I'm looking for a little guidance. Until this morning, this was all over my head. After spending today researching Wikipedia, StackOverflow, etc., I'd say I've got my nose above the water. I'm tasked ...
user avatar
  • 151
4 votes
4 answers
13k views

Regex to String generation

Let's say that we have a regex and an index i. If we suppose that the set of strings that match our regex are sorted in a lexicographical order, how could we get the i element of this list? Edit : I ...
user avatar
  • 129
3 votes
1 answer
1k views

Why does the regular expression metacharacter \w include underscores?

In most, if not all implementations of regular expressions, the \w metacharacter matches all alphanumeric characters plus the underscore. Historically speaking, why was the underscore character ...
user avatar
  • 133
-2 votes
2 answers
990 views

Implementing a basic interpreter - should I learn regular expressions? [closed]

For fun and for practice, I'm thinking of maybe implementing a very basic programming language to run on a simple interpreter written in Java. My question is this: Is it a 'must' to know regular ...
user avatar
  • 20.7k
0 votes
2 answers
3k views

Word recognition in a string without spaces or punctuation marks

I have a small C# project that reads a file and gives me an output: a string that does not contain spaces nor any types of punctuation marks. It may also contain a few misspellings. Ex. Output: ...
user avatar
  • 111
0 votes
1 answer
3k views

What is the right way to parse HTML? [closed]

I've heard, that parsing HTML using the Cthulhu way is not very good. But what are the right ways to parse HTML? Or is it possible to parse it at all?
user avatar
  • 119
15 votes
7 answers
2k views

Should I refactor large functions that mostly consist of one regex? [closed]

I just wrote a function that spans approximately 100 lines. Hearing that, you are probably tempted to tell me about single responsibilities and urge me to refactor. This is my gut instinct as well, ...
user avatar
  • 1,069