Questions tagged [regex]

Regular expressions provide a declarative language to match patterns within strings. They are commonly used for string validation, parsing, and transformation. Specify the language (PHP, Python, etc) or tool (grep, VS Code, Google Analytics, etc) that you are using. Do not post questions asking for an explanation of what a symbol means or what a particular regular expression will match.

Filter by
Sorted by
Tagged with
2210 votes
36 answers
3.7m views

RegEx match open tags except XHTML self-contained tags

I need to match all of these opening tags: <p> <a href="foo"> But not these: <br /> <hr class="foo" /> I came up with this and wanted to make sure I've got it right. I am ...
52 votes
1 answer
198k views

Reference - What does this regex mean?

What is this? This is a collection of common Q&A. This is also a Community Wiki, so everyone is invited to participate in maintaining it. Why is this? regex is suffering from give me ze code type ...
4101 votes
73 answers
2.4m views

How can I validate an email address using a regular expression?

Over the years I have slowly developed a regular expression that validates most email addresses correctly, assuming they don't use an IP address as the server part. I use it in several PHP programs, ...
119 votes
4 answers
78k views

My regex is matching too much. How do I make it stop? [duplicate]

I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM J0000010: Project name: E:\foo.pf J0000011: Job name: MBiek Direct Mail Test J0000020: Document 1 - ...
Mark Biek's user avatar
  • 147k
5476 votes
79 answers
4.6m views

How can I validate an email address in JavaScript?

I'd like to check if the user input is an email address in JavaScript, before sending it to a server or attempting to send an email to it, to prevent the most basic mistyping. How could I achieve this?...
166 votes
1 answer
152k views

Learning Regular Expressions [closed]

I don't really understand regular expressions. Can you explain them to me in an easy-to-follow manner? If there are any online tools or books, could you also link to them?
2410 votes
18 answers
1.2m views

What is a non-capturing group in regular expressions?

How are non-capturing groups, i.e., (?:), used in regular expressions and what are they good for?
never_had_a_name's user avatar
5284 votes
34 answers
4.9m views

Regular expression to match a line that doesn't contain a word

I know it's possible to match a word and then reverse the matches using other tools (e.g. grep -v). However, is it possible to match lines that do not contain a specific word, e.g. hede, using a ...
1053 votes
45 answers
1.2m views

How to validate phone numbers using regex

I'm trying to put together a comprehensive regex to validate phone numbers. Ideally it would handle international formats, but it must handle US formats, including the following: 1-234-567-8901 1-234-...
743 votes
9 answers
1.3m views

How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops

How can I use regular expressions in Excel and take advantage of Excel's powerful grid-like setup for data manipulation? In-cell function to return a matched pattern or replaced value in a string. ...
Automate This's user avatar
464 votes
13 answers
340k views

What special characters must be escaped in regular expressions?

I am tired of always trying to guess, if I should escape special characters like '()[]{}|' etc. when using many implementations of regexps. It is different with, for example, Python, sed, grep, awk, ...
Igor Katson's user avatar
  • 5,227
236 votes
18 answers
78k views

Using regular expressions to parse HTML: why not?

It seems like every question on stackoverflow where the asker is using regex to grab some information from HTML will inevitably have an "answer" that says not to use regex to parse HTML. Why not? I'm ...
ntownsend's user avatar
  • 7,462
606 votes
18 answers
190k views

Is there a RegExp.escape function in JavaScript?

I just want to create a regular expression out of any possible string. var usersString = "Hello?!*`~World()[]"; var expression = new RegExp(RegExp.escape(usersString)) var matches = "...
Lance's user avatar
  • 75.4k
1031 votes
64 answers
674k views

What is the best regular expression to check if a string is a valid URL?

How can I check if a given string is a valid URL address? My knowledge of regular expressions is basic and doesn't allow me to choose from the hundreds of regular expressions I've already seen on the ...
Vitor Silva's user avatar
  • 17.1k
359 votes
7 answers
63k views

Why does a RegExp with global flag give wrong results?

What is the problem with this regular expression when I use the global flag and the case insensitive flag? Query is a user generated input. The result should be [true, true]. var query = 'Foo B'; var ...
about's user avatar
  • 3,857
587 votes
26 answers
976k views

How do I match any character across multiple lines in a regular expression?

For example, this regex (.*)<FooBar> will match: abcde<FooBar> But how do I get it to match across multiple lines? abcde fghij<FooBar>
andyuk's user avatar
  • 38.2k
1899 votes
27 answers
1.2m views

How do you use a variable in a regular expression?

I would like to create a String.replaceAll() method in JavaScript and I'm thinking that using a regex would be most terse way to do it. However, I can't figure out how to pass a variable in to a ...
JC Grubbs's user avatar
  • 39.3k
76 votes
5 answers
22k views

Why do regex constructors need to be double escaped?

In the regex below, \s denotes a space character. I imagine the regex parser, is going through the string and sees \ and knows that the next character is special. But this is not the case as double ...
Smurfette's user avatar
  • 1,945
783 votes
13 answers
431k views

What do 'lazy' and 'greedy' mean in the context of regular expressions?

What are these two terms in an understandable way?
ajsie's user avatar
  • 77.7k
201 votes
8 answers
69k views

What to do when a regular expression pattern doesn't match anywhere in a string?

I am trying to match <input> of type hidden fields using this pattern: /<input type="hidden" name="([^"]*?)" value="([^"]*?)" />/ This is some ...
Salman's user avatar
  • 2,119
79 votes
4 answers
30k views

Is it possible to escape regex metacharacters reliably with sed

I'm wondering whether it is possible to write a 100% reliable sed command to escape any regex metacharacters in an input string so that it can be used in a subsequent sed command. Like this: #!/bin/...
hek2mgl's user avatar
  • 152k
1711 votes
23 answers
1.0m views

How do you access the matched groups in a JavaScript regular expression?

I want to match a portion of a string using a regular expression and then access that parenthesized substring: var myString = "something format_abc"; // I want "abc" var arr = /(?:^|\s)format_(.*?...
nickf's user avatar
  • 537k
415 votes
12 answers
51k views

Can you provide some examples of why it is hard to parse XML and HTML with a regex? [closed]

One mistake I see people making over and over again is trying to parse XML or HTML with a regex. Here are a few of the reasons parsing XML and HTML is hard: People want to treat a file as a sequence ...
Chas. Owens's user avatar
  • 64.2k
32 votes
3 answers
7k views

re.findall behaves weird

The source string is: # Python 3.4.3 s = r'abc123d, hello 3.1415926, this is my book' and here is my pattern: pattern = r'-?[0-9]+(\\.[0-9]*)?|-?\\.[0-9]+' however, re.search can give me correct ...
O'Skywalker's user avatar
143 votes
10 answers
39k views

Why it's not possible to use regex to parse HTML/XML: a formal explanation in layman's terms

There is no day on SO that passes without a question about parsing (X)HTML or XML with regular expressions being asked. While it's relatively easy to come up with examples that demonstrates the non-...
mac's user avatar
  • 42.2k
20 votes
3 answers
6k views

Tempered Greedy Token - What is different about placing the dot before the negative lookahead?

<table((?!</table>).)*</table> matches all my table tags. However, <table(.(?!</table>))*</table> does not. The second one seems to make sense if I try to write out ...
jmrah's user avatar
  • 5,715
675 votes
10 answers
405k views

What is the difference between re.search and re.match?

What is the difference between the search() and match() functions in the Python re module? I've read the Python 2 documentation (Python 3 documentation), but I never seem to remember it.
Daryl Spitzer's user avatar
520 votes
6 answers
816k views

Regex: match everything but a specific pattern

I need a regular expression able to match everything but a string starting with a specific pattern (specifically index.php and what follows, like index.php?id=2342343).
pistacchio's user avatar
  • 57.1k
802 votes
9 answers
1.0m views

Regular expression to stop at first match

My regex pattern looks something like <xxxx location="file path/level1/level2" xxxx some="xxx"> I am only interested in the part in quotes assigned to location. Shouldn't it be as easy as ...
101 votes
3 answers
177k views

How do I deal with special characters like \^$.?*|+()[{ in my regex?

I want to match a regular expression special character, \^$.?*|+()[{. I tried: x <- "a[b" grepl("[", x) ## Error: invalid regular expression '[', reason 'Missing ']'' (Equivalently stringr::...
Richie Cotton's user avatar
231 votes
3 answers
231k views

Match exact string

What is the regular expression (in JavaScript if it matters) to only match if the text is an exact match? That is, there should be no extra characters at other end of the string. For example, if I'm ...
Jake Pearson's user avatar
  • 27.1k
603 votes
13 answers
605k views

How to do a regular expression replace in MySQL?

I have a table with ~500k rows; varchar(255) UTF8 column filename contains a file name; I'm trying to strip out various strange characters out of the filename - thought I'd use a character class: [^a-...
Piskvor left the building's user avatar
419 votes
21 answers
498k views

Regular expression to match balanced parentheses

I need a regular expression to select all the text between two outer brackets. Example: START_TEXT(text here(possible text)text(possible text(more text)))END_TXT ^ ...
DaveF's user avatar
  • 6,559
242 votes
11 answers
145k views

Can regular expressions be used to match nested patterns? [duplicate]

Is it possible to write a regular expression that matches a nested pattern that occurs an unknown number of times? For example, can a regular expression match an opening and closing brace when there ...
Richard Dorman's user avatar
71 votes
3 answers
58k views

Warning: preg_replace(): Unknown modifier

I have the following error: Warning: preg_replace(): Unknown modifier ']' in xxx.php on line 38 This is the code on line 38: <?php echo str_replace("</ul></div>", "", preg_replace("...
user3122995's user avatar
534 votes
5 answers
453k views

Regex lookahead, lookbehind and atomic groups

I found these things in my regex body but I haven't got a clue what I can use them for. Does somebody have examples so I can try to understand how they work? (?!) - negative lookahead (?=) - positive ...
35 votes
2 answers
11k views

How do (*SKIP) or (*F) work on regex?

I'm learning an advanced usage of regex and noticed that many posts use (*SKIP) or (*F) in it. I posted a question where the idea was to match lines that don't have yellow but has blue only if brown ...
Federico Piazza's user avatar
107 votes
3 answers
39k views

Is gcc 4.8 or earlier buggy about regular expressions?

I am trying to use std::regex in a C++11 piece of code, but it appears that the support is a bit buggy. An example: #include <regex> #include <iostream> int main (int argc, const char * ...
tunnuz's user avatar
  • 23.4k
883 votes
43 answers
1.3m views

Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters

I want a regular expression to check that: A password contains at least eight characters, including at least one number and includes both lower and uppercase letters and special characters, for ...
Swapnil Tatkondawar's user avatar
148 votes
5 answers
95k views

How to use regex to find all overlapping matches

I'm trying to find every 10 digit series of numbers within a larger series of numbers using re in Python 2.6. I'm easily able to grab no overlapping matches, but I want every match in the number ...
danspants's user avatar
  • 3,119
117 votes
6 answers
41k views

Regex Pattern to Match, Excluding when... / Except between

--Edit-- The current answers have some useful ideas but I want something more complete that I can 100% understand and reuse; that's why I set a bounty. Also ideas that work everywhere are better for ...
Hans Schindler's user avatar
490 votes
25 answers
298k views

How to replace plain URLs with links?

I am using the function below to match URLs inside a given text and replace them for HTML links. The regular expression is working great, but currently I am only replacing the first match. How I can ...
Sergio del Amo's user avatar
56 votes
3 answers
6k views

Why is this regex allowing a caret?

http://regexr.com/3ars8 ^(?=.*[0-9])(?=.*[A-z])[0-9A-z-]{17}$ Should match "17 alphanumeric chars, hyphens allowed too, must include at least one letter and at least one number" It'll correctly ...
Wintermute's user avatar
  • 2,983
285 votes
12 answers
258k views

Java: splitting a comma-separated string but ignoring commas in quotes

I have a string vaguely like this: foo,bar,c;qual="baz,blurb",d;junk="quux,syzygy" that I want to split by commas -- but I need to ignore commas in quotes. How can I do this? ...
Jason S's user avatar
  • 185k
675 votes
1 answer
422k views

Escape string for use in Javascript regex [duplicate]

Possible Duplicate: Is there a RegExp.escape function in Javascript? I am trying to build a javascript regex based on user input: function FindString(input) { var reg = new RegExp('' + input ...
too much php's user avatar
  • 88.8k
259 votes
11 answers
179k views

How can I use Unicode-aware regular expressions in JavaScript?

There should be something akin to \w that can match any code-point in Letters or Marks category (not just the ASCII ones), and hopefully have filters like [[P*]] for punctuation, etc.
user avatar
691 votes
18 answers
1.3m views

Regex Match all characters between two strings

Example: This is just\na simple sentence. I want to match every character between This is and sentence. Line breaks should be ignored. I can't figure out the correct syntax.
0xbadf00d's user avatar
  • 17.5k
69 votes
4 answers
145k views

How to match a whole word with a regular expression?

I'm having trouble finding the correct regular expression for the scenario below: Lets say: a = "this is a sample" I want to match whole word - for example match "hi" should return False since "hi" ...
user2161049's user avatar
252 votes
13 answers
634k views

Regular expression to get a string between two strings in Javascript

I have found very similar posts, but I can't quite get my regular expression right here. I am trying to write a regular expression which returns a string which is between two other strings. For ...
phil's user avatar
  • 3,538
259 votes
28 answers
1.0m views

Regex to validate date formats dd/mm/YYYY, dd-mm-YYYY, dd.mm.YYYY, dd mmm YYYY, dd-mmm-YYYY, dd/mmm/YYYY, dd.mmm.YYYY with Leap Year Support

I need to validate a date string for the format dd/mm/yyyy with a regular expresssion. This regex validates dd/mm/yyyy, but not the invalid dates like 31/02/4500: ^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[...
Nalaka526's user avatar
  • 11.3k

1
2 3 4 5
464