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,594
questions
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?...
5397
votes
34
answers
5.2m
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 ...
4201
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, ...
2462
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?
2275
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 ...
1936
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 ...
1739
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_(.*?...
1346
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]'
1346
votes
4
answers
108k
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 ...
1180
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 ...
1163
votes
10
answers
266k
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.
1063
votes
65
answers
722k
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 ...
1058
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-...
1021
votes
15
answers
1.3m
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 ...
911
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:
contains at least eight characters,
including at least one number and
includes both lower and uppercase letters and
include at least one special characters, ...