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.

regex
Filter by
Sorted by
Tagged with
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?
never_had_a_name's user avatar
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 ...
JC Grubbs's user avatar
  • 39.7k
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_(.*?...
nickf's user avatar
  • 541k
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 ...
weston's user avatar
  • 54.5k
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]'
jerrygarciuh's user avatar
  • 21.5k
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 ...
Richard's user avatar
  • 32.3k
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.
psytek's user avatar
  • 9,101
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 ...
Vitor Silva's user avatar
  • 17.3k
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 ...
hugoware's user avatar
  • 36.1k
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 ...
Swapnil Tatkondawar's user avatar
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. ...
euforia's user avatar
  • 8,955
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 ...
callum's user avatar
  • 36k
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")); //...
TruMan1's user avatar
  • 34.6k
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 ...
Bostone's user avatar
  • 36.9k
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?
user avatar
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?
ajsie's user avatar
  • 78.7k
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 ...
Orjanp's user avatar
  • 10.7k
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,...
AnApprentice's user avatar
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?
Luke101's user avatar
  • 64.1k
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.
user avatar
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. ...
Automate This's user avatar
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+))?(?:\/([^?#]*))?(?:...
bigbob's user avatar
  • 7,499
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.
0xbadf00d's user avatar
  • 17.8k
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?
TIMEX's user avatar
  • 265k
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 ...
too much php's user avatar
  • 89.8k
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.
Daryl Spitzer's user avatar
661 votes
9 answers
1.1m views

How can I exclude one word with grep?

I need something like: grep ^"unwanted_word"XXXXXXXX
john's user avatar
  • 6,645
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 ....
Wingblade's user avatar
  • 9,825
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.
Chris Bunch's user avatar
  • 88.7k
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", "...
siamii's user avatar
  • 23.8k
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 ...
Joel Cunningham's user avatar
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')
Mat's user avatar
  • 84.2k
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 ...
ObiWanKenobi's user avatar
  • 14.8k
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 = "...
Lance's user avatar
  • 77.1k
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-...
Piskvor left the building's user avatar
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>
andyuk's user avatar
  • 38.6k
609 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,131
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 ...
Leon Fedotov's user avatar
  • 7,651
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
Pokus's user avatar
  • 11.6k
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;">...
Mark Biek's user avatar
  • 149k
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].*
brother's user avatar
  • 7,881
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); ...
user1022585's user avatar
  • 13.4k
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-...
Troy Rockwood's user avatar

1
2 3 4 5
5207