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
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?...
5286 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 ...
4102 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, ...
2411 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
2211 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 ...
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
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
  • 538k
1338 votes
4 answers
105k 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 ...
weston's user avatar
  • 54.2k
1303 votes
3 answers
1.2m views

Negative matching using grep (match lines that do not contain foo)

How do I match all lines not matching a particular pattern using grep? I tried this: grep '[^foo]'
jerrygarciuh's user avatar
  • 21.2k
1153 votes
10 answers
257k 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.
psytek's user avatar
  • 9,051
1144 votes
15 answers
1.4m 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 ...
Richard's user avatar
  • 31.7k
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-...
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
987 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 ...
hugoware's user avatar
  • 35.8k
884 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
843 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 ...
callum's user avatar
  • 34.5k
832 votes
41 answers
740k 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")); //...
TruMan1's user avatar
  • 33.8k
832 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 ...
Bostone's user avatar
  • 36.9k
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 ...
798 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?
user avatar
791 votes
31 answers
710k 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 ...
Orjanp's user avatar
  • 10.7k
783 votes
13 answers
432k 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.8k
770 votes
27 answers
703k 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,...
AnApprentice's user avatar
766 votes
13 answers
500k 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?
Luke101's user avatar
  • 63.2k
747 votes
25 answers
839k 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.
user avatar
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
726 votes
5 answers
1.3m 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+))?(?:\/([^?#]*))?(?:...
bigbob's user avatar
  • 7,269
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
679 votes
27 answers
693k 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?
TIMEX's user avatar
  • 261k
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
675 votes
10 answers
406k 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
651 votes
6 answers
226k 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.
Chris Bunch's user avatar
  • 87.9k
650 votes
9 answers
1.0m views

How can I exclude one word with grep?

I need something like: grep ^"unwanted_word"XXXXXXXX
john's user avatar
  • 6,535
643 votes
21 answers
1.0m 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 ....
Wingblade's user avatar
  • 9,635
643 votes
10 answers
491k 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 ...
Joel Cunningham's user avatar
640 votes
4 answers
1.1m 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", "...
siamii's user avatar
  • 23.4k
619 votes
28 answers
413k 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')
Mat's user avatar
  • 82.5k
610 votes
15 answers
992k 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 ...
ObiWanKenobi's user avatar
  • 14.5k
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
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
596 votes
23 answers
1.3m views

Regex to match only letters

How can I write a regex that matches only letters?
Nike's user avatar
  • 6,001
588 votes
26 answers
977k 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
580 votes
15 answers
903k 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 ...
Leon Fedotov's user avatar
  • 7,501
574 votes
28 answers
383k 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
Pokus's user avatar
  • 11.4k
569 votes
8 answers
153k 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;">...
Mark Biek's user avatar
  • 147k
548 votes
15 answers
1.3m 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].*
brother's user avatar
  • 7,701
536 votes
27 answers
853k 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); ...
user1022585's user avatar
  • 13.1k
536 votes
4 answers
1.1m 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-...
Troy Rockwood's user avatar
536 votes
10 answers
536k views

Case insensitive regular expression without re.compile?

In Python, I can compile a regular expression to be case-insensitive using re.compile: >>> s = 'TeSt' >>> casesensitive = re.compile('test') >>> ignorecase = re.compile('...
Mat's user avatar
  • 82.5k
535 votes
5 answers
454k 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 ...

1
2 3 4 5
5187