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
12 votes
2 answers
2k views

Designing a Python string validation library

My employer has a significant number of company-internal strings which require format validation. For instance, order number AAA-BBB-CCC, stock number AB-123456 or factory ABC1 - Regex with extras (...
MikeFoxtrot's user avatar
1 vote
1 answer
132 views

How to implement a RegEx library in C [closed]

Background I have been attempting to create a line editor in C, eventually becoming a visual editor, and as a means of both allowing the editor to be useful but also learn more about C development, I ...
greybrunix's user avatar
-1 votes
2 answers
232 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 ...
GreenTriangle's user avatar
31 votes
6 answers
12k 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&...
夢のの夢's user avatar
2 votes
2 answers
3k 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'...
sunyata's user avatar
  • 449
0 votes
0 answers
158 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 ...
flowit's user avatar
  • 237
-5 votes
1 answer
790 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?
gython's user avatar
  • 93
3 votes
3 answers
2k 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 ...
inf3rno's user avatar
  • 1,209
-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 ...
Evan Carroll's user avatar
0 votes
1 answer
47 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: ...
ShapeOfMatter's user avatar
1 vote
1 answer
194 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 ...
mathnoobie's user avatar
2 votes
4 answers
2k 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....
Anon's user avatar
  • 3,575
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 ...
fortune's user avatar
  • 157
3 votes
1 answer
180 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 ...
AbstProcDo's user avatar
9 votes
4 answers
448 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?
icirellik's user avatar
  • 429
4 votes
1 answer
983 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 ...
Lupo Dharkael's user avatar
6 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 ...
ceving's user avatar
  • 381
10 votes
5 answers
785 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 ...
Anon's user avatar
  • 3,575
-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 ...
JamesC's user avatar
  • 11
4 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 ...
Christian Dean's user avatar
2 votes
2 answers
15k 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 ...
Chris Cirefice's user avatar
7 votes
2 answers
265 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, ...
Chris's user avatar
  • 207
0 votes
1 answer
85 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?
chx's user avatar
  • 363
0 votes
4 answers
824 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!): ^...
Tomáš Zato's user avatar
0 votes
1 answer
286 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, ...
aebabis's user avatar
  • 103
35 votes
6 answers
6k 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 ...
Lii's user avatar
  • 461
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 ...
Niklas Rosencrantz's user avatar
7 votes
2 answers
7k 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 ...
Yannis P.'s user avatar
  • 171
-1 votes
1 answer
334 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 ...
Nexima360's user avatar
-2 votes
2 answers
405 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 ...
M. Doe's 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 ...
sigil's 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 ...
Josh Broadhurst's user avatar
1 vote
1 answer
429 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 ...
user4951's user avatar
  • 699
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 ...
J.Todd's user avatar
  • 3,833
2 votes
1 answer
205 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 ...
Dehbop's user avatar
  • 169
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 ...
Gigi Ionel's user avatar
0 votes
1 answer
160 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#?
Dilan V's user avatar
  • 111
1 vote
1 answer
247 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: ...
Component 10's user avatar
3 votes
1 answer
245 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 ...
AnotherGuy's user avatar
0 votes
1 answer
382 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 ...
Wojciech Szczurek's user avatar
1 vote
0 answers
213 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. ...
Regular Jo's user avatar
1 vote
1 answer
721 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'...
Viacheslav Kondratiuk's 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,...
GermanShepherd's user avatar
1 vote
2 answers
583 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 ...
clambake's user avatar
  • 133
4 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 ...
Sidharth Ghoshal's user avatar
5 votes
2 answers
372 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 ...
Mario's user avatar
  • 151
4 votes
4 answers
14k 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 ...
Mifmif's 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 ...
Alex's user avatar
  • 133
-2 votes
2 answers
1k 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 ...
Aviv Cohn's user avatar
  • 21.2k
0 votes
2 answers
4k 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: ...
RKrogh's user avatar
  • 111