23,444
questions
2283
votes
36
answers
3.9m
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 self-closing tags:
<br />
<hr class="foo" />
I came up with this and wanted to make ...
52
votes
1
answer
213k
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 ...
4216
votes
78
answers
2.6m
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, ...
123
votes
4
answers
85k
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 - ...
166
votes
1
answer
156k
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?
5471
votes
79
answers
4.8m
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?...
5412
votes
34
answers
5.3m
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 ...
2468
votes
18
answers
1.3m
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?
1059
votes
46
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-...
758
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.
...
483
votes
14
answers
367k
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, ...
243
votes
18
answers
82k
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 ...
623
votes
19
answers
204k
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 = "...
1071
votes
65
answers
729k
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 ...
378
votes
7
answers
66k
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 ...
626
votes
26
answers
1.1m
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>
828
votes
13
answers
484k
views
What do 'lazy' and 'greedy' mean in the context of regular expressions?
What are these two terms in an understandable way?
1946
votes
29
answers
1.3m
views
How do you use a variable in a regular expression?
I would like to create a String.replaceAll() method in JavaScript and think using a regex would be the most terse way to do it. However, I can't figure out how to pass a variable into a regex. I can ...
79
votes
5
answers
23k
views
Why do strings given to the RegExp constructor 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 ...
84
votes
4
answers
33k
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/...
202
votes
8
answers
70k
views
What can I 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 ...
1745
votes
23
answers
1.1m
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_(.*?...
148
votes
10
answers
41k
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-...
36
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 ...
414
votes
12
answers
52k
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 ...
21
votes
3
answers
7k
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 ...
547
votes
6
answers
887k
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).
700
votes
10
answers
432k
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.
834
votes
10
answers
1.1m
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 ...
240
votes
3
answers
245k
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 ...
106
votes
3
answers
189k
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::...
619
votes
13
answers
626k
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-...
437
votes
22
answers
522k
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
^ ...
243
votes
11
answers
152k
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 ...
626
votes
5
answers
543k
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?
(?=) - positive lookahead
(?!) - negative ...
74
votes
3
answers
62k
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("...
38
votes
2
answers
12k
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 ...
107
votes
3
answers
40k
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 * ...
168
votes
4
answers
112k
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 ...
915
votes
43
answers
1.5m
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:
contains at least eight characters,
including at least one number and
includes both lower and uppercase letters and
include at least one special characters, ...
118
votes
6
answers
43k
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 ...
909
votes
18
answers
1.5m
views
Filter pandas DataFrame by substring criteria
I have a pandas DataFrame with a column of string values. I need to select rows based on partial string matches.
Something like this idiom:
re.search(pattern, cell_in_question)
returning a boolean. ...
493
votes
26
answers
301k
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 ...
55
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 ...
696
votes
1
answer
448k
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 ...
285
votes
13
answers
265k
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? ...
264
votes
11
answers
187k
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.
717
votes
18
answers
1.4m
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.
73
votes
4
answers
157k
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" ...
157
votes
8
answers
489k
views
Regex match entire words only
I have a regex expression that I'm using to find all the words in a given block of content, case insensitive, that are contained in a glossary stored in a database. Here's my pattern:
/($word)/i
The ...