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.
260,302
questions
5474
votes
79
answers
4.7m
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?...
5357
votes
35
answers
5.1m
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 ...
4168
votes
77
answers
2.5m
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, ...
2445
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?
2238
votes
37
answers
3.8m
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 ...
1923
votes
28
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 ...
1731
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_(.*?...
1343
votes
4
answers
107k
views
\d less efficient than [0-9]
I made a comment yesterday on an answer where someone had used [0123456789] in a regex rather than [0-9] or \d. I said it was probably more efficient to use a range or digit specifier than a character ...
1327
votes
3
answers
1.3m
views
Negative matching using grep (match lines that do not contain foo) [duplicate]
How do I match all lines not matching a particular pattern using grep? I tried this:
grep '[^foo]'
1165
votes
15
answers
1.5m
views
Check whether a string matches a regex in JS
I want to use JavaScript (I can also use jQuery) to do check whether a string matches the regex ^([a-z0-9]{5,})$, and get a true or false result.
match() seems to check whether part of a string ...
1158
votes
10
answers
261k
views
Is there a regular expression to detect a valid regular expression?
Is it possible to detect a valid regular expression with another regular expression? If so please give example code below.
1056
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-...
1051
votes
65
answers
700k
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 ...
1009
votes
15
answers
1.2m
views
Regular Expressions: Is there an AND operator?
Obviously, you can use the | (pipe?) to represent OR, but is there a way to represent AND as well?
Specifically, I'd like to match paragraphs of text that contain ALL of a certain phrase, but in no ...
904
votes
43
answers
1.4m
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 ...
887
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. ...
859
votes
15
answers
1.3m
views
How can I match "anything up until this sequence of characters" in a regular expression?
Take this regular expression: /^[^abc]/. This will match any single character at the beginning of a string, except a, b, or c.
If you add a * after it – /^[^abc]*/ – the regular expression will ...
847
votes
41
answers
763k
views
How to count string occurrence in string?
How can I count the number of times a particular string occurs in another string. For example, this is what I am trying to do in Javascript:
var temp = "This is a string.";
alert(temp.count("is")); //...
843
votes
12
answers
1.1m
views
How to negate specific word in regex? [duplicate]
I know that I can negate group of chars as in [^bar] but I need a regular expression where negation applies to the specific word - so in my example how do I negate an actual bar, and not "any chars in ...
822
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 ...
812
votes
21
answers
1.7m
views
Regular expression for alphanumeric and underscores
Is there a regular expression which checks if a string contains only upper and lowercase letters, numbers, and underscores?
809
votes
13
answers
458k
views
What do 'lazy' and 'greedy' mean in the context of regular expressions?
What are these two terms in an understandable way?
795
votes
31
answers
734k
views
Find and kill a process in one line using bash and regex
I often need to kill a process during programming.
The way I do it now is:
[~]$ ps aux | grep 'python csp_build.py'
user 5124 1.0 0.3 214588 13852 pts/4 Sl+ 11:19 0:00 python csp_build.py
...
783
votes
27
answers
730k
views
Regex to replace multiple spaces with a single space
Given a string like:
"The dog has a long tail, and it is RED!"
What kind of jQuery or JavaScript magic can be used to keep spaces to only one space max?
Goal:
"The dog has a long tail,...
777
votes
13
answers
519k
views
How do I remove all non alphanumeric characters from a string except dash?
How do I remove all non alphanumeric characters from a string except dash and space characters?
768
votes
26
answers
872k
views
How do I split a string with multiple separators in JavaScript?
How do I split a string with multiple separators in JavaScript?
I'm trying to split on both commas and spaces, but AFAIK JavaScript's split() function only supports one separator.
751
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.
...
749
votes
5
answers
1.4m
views
What is a good regular expression to match a URL? [duplicate]
Currently I have an input box which will detect the URL and parse the data.
So right now, I am using:
var urlR = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)
(?::(\d+))?(?:\/([^?#]*))?(?:...
704
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.
697
votes
27
answers
730k
views
Is there a simple way to remove multiple spaces in a string?
Suppose this string:
The fox jumped over the log.
Turning into:
The fox jumped over the log.
What is the simplest (1-2 lines) to achieve this, without splitting and going into lists?
689
votes
1
answer
437k
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 ...
686
votes
10
answers
418k
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.
661
votes
9
answers
1.1m
views
How can I exclude one word with grep?
I need something like:
grep ^"unwanted_word"XXXXXXXX
658
votes
21
answers
1.1m
views
How to remove all line breaks from a string
I have a text in a textarea and I read it out using the .value attribute.
Now I would like to remove all linebreaks (the character that is produced when you press Enter) from my text now using ....
656
votes
6
answers
230k
views
How to match all occurrences of a regular expression in Ruby
Is there a quick way to find every match of a regular expression in Ruby? I've looked through the Regex object in the Ruby STL and searched on Google to no avail.
655
votes
4
answers
1.2m
views
Split string on whitespace in Python [duplicate]
I'm looking for the Python equivalent of
String str = "many fancy word \nhello \thi";
String whiteSpaceRegex = "\\s";
String[] words = str.split(whiteSpaceRegex);
["many", "fancy", "word", "...
645
votes
10
answers
496k
views
jQuery selector regular expressions
I am after documentation on using wildcard or regular expressions (not sure on the exact terminology) with a jQuery selector.
I have looked for this myself but have been unable to find information on ...
630
votes
28
answers
425k
views
Is it worth using Python's re.compile?
Is there any benefit in using compile for regular expressions in Python?
h = re.compile('hello')
h.match('hello world')
vs
re.match('hello', 'hello world')
628
votes
15
answers
1.0m
views
Regular expression to extract text between square brackets
Simple regex question. I have a string on the following format:
this is a [sample] string with [some] special words. [another one]
What is the regular expression to extract the words within the ...
621
votes
20
answers
198k
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 = "...
613
votes
13
answers
616k
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-...
611
votes
26
answers
1.0m
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>
609
votes
23
answers
1.3m
views
Regex to match only letters
How can I write a regex that matches only letters?
598
votes
16
answers
948k
views
Regex: matching up to the first occurrence of a character
I am looking for a pattern that matches everything until the first occurrence of a specific character, say a ";" - a semicolon.
I wrote this:
/^(.*);/
But it actually matches everything (including ...
579
votes
28
answers
393k
views
How do I replace multiple spaces with a single space in C#?
How can I replace multiple spaces in a string with only one space in C#?
Example:
1 2 3 4 5
would be:
1 2 3 4 5
579
votes
8
answers
160k
views
How can I make my match non greedy in vim?
I have a big HTML file that has lots of markup that looks like this:
<p class="MsoNormal" style="margin: 0in 0in 0pt;">
<span style="font-size: small; font-family: Times New Roman;">...
578
votes
5
answers
493k
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 ...
564
votes
15
answers
1.4m
views
Regex: ignore case sensitivity
How can I make the following regex ignore case sensitivity? It should match all the correct characters but ignore whether they are lower or uppercase.
G[a-b].*
552
votes
27
answers
893k
views
How can I extract a number from a string in JavaScript?
I have a string in JavaScript (e.g., #box2), and I just want the 2 from it.
I tried:
var thestring = $(this).attr('href');
var thenum = thestring.replace(/(^.+)(\w\d+\w)(.+$)/i, '$2');
alert(thenum);
...
547
votes
4
answers
1.2m
views
Python string.replace regular expression [duplicate]
I have a parameter file of the form:
parameter-name parameter-value
Where the parameters may be in any order but there is only one parameter per line. I want to replace one parameter's parameter-...