Questions tagged [regex]

Also known as regular expression or regexp, a description of a set of strings, often used for searching and validating strings.

A regex, also known as a regular expression or regexp, is a description of a set of strings with certain properties. For example, \d{5} matches a series of 5 digits, such as 43822.

Use this tag when on the hunt for scripting syntax (in , , , , , etc.) which will match a given set of text.

Because regular expressions are not fully standardized, all questions with this tag should also include a tag specifying the applicable programming language or tool.

2090 questions
63
votes
7 answers

Matching only the first occurrence in a line with Regex

I am completely new to regex and I would greatly appreciate any help. The task is simple. I have a CSV file with records that read like…
47
votes
7 answers

How should I write a regex to match a specific word?

I've been trying to get a specific regex working but I can't get it to do what I need. Basically, I want it to look for ROCKET. The regex should match ROCKET in upper or lower cases, and with or without punctuation, but not when part of another…
Kefka
  • 1,436
  • 2
  • 15
  • 30
35
votes
2 answers

Regex matching line not containing the string

I'd like to select a lines with does not contain a word SCREEN. I tried the following regex (?
Ringger81
  • 1,157
  • 3
  • 11
  • 25
20
votes
3 answers

What does "Please enter a username matching the regular expression configured via the NAME_REGEX[_SYSTEM]" mean?

I am a research assistant in a neuroscience lab completely new to programming. I'm just trying to make a username in the ubuntu system I downloaded to run an MRI analysis program for someone else. And I don't even understand what this basic command…
Alex
  • 201
16
votes
1 answer

Why there are so many regular expression variants?

Today when I reviewed our project's source code, I found there are lots of unnecessary "spaces" and "tabs" located at each line's end. So I decided to delete them with regular expression. However, I found the command sed -i '/\s+$/d' doesn't…
hero2008
  • 637
11
votes
2 answers

Extracting final word in sentence through regular expression?

I can't find a way to extract the final word on a line, such as this: ^This is a sentence$ I'd like to extract just "sentence". Does someone have an idea? Thank you.
11
votes
1 answer

Regex to match XML comments

I am looking for a Regex to match comments in XML documents: not this I've tried )]*-->, which only matches single line comments, and which…
8
votes
1 answer

How to use REGEX to capitalise the first letter of each word in a sentence?

I would like to use regular expressions (REGEX) to capitalise the first letter of each word in a sentence. I have achieved the same result in programming languages, but it seems that using regular expressions would be more concise.
8
votes
1 answer

How to match a word followed by anything in Notepad++

I want to enclose the first word with brackets and the "anything" that follows after the space by brackets too. Unfortunately the command : (\w) (.*) doesn't work. For example, for the sentence "How to match a word followed by anything " it will…
mdrlol
  • 167
6
votes
2 answers

Regular Expressions: How is group matching useful?

I've decided to learn some regular expression basics. I am using the Regex One lessons online and I was stuck at lession 11 for a while, but I think I got it now. This was the task. "Write a regular expression that matches only the filenames (not…
Samir
  • 20,845
6
votes
1 answer

Regular Expression - Where string contains 'bar' but is not preceded by 'foo'

I currently have a regular expression to match where a string contains 'bar' but is not preceded by 'foo': (?
Fitzroy
  • 331
5
votes
2 answers

Regex: match a pattern, change a part of it

If you have a text file and want to search and replace a part of a matched pattern, how would you do it with perl one-liners, sed or python? For example: "653433,78" match with [0-9]{6},[0-9]{2}. Change , to ..
5
votes
2 answers

Matching second last word in sentence through regular expression?

I'm looking for a way to match the second last word on a line, such as this: 123 Smith St Melbourne VIC 3000 I'd like to match just "VIC". Does someone have a regex I can use?
John
  • 51
5
votes
1 answer

Regex positive lookahead when lookahead data doesn't exist (PCRE)

I'm trying to match out a string which will have a format similar to the following: username=joe password='this! is a password with "quotes" and spaces#913 custom1 customd afd weirdhuh? custom1=myvalue To explain in more detail, this is a list of…
BenH
  • 53
4
votes
2 answers

Regular expression that finds same words within 'n' words of each other

I'm trying to put together a regular expression search that finds any two (or more) words that are within n (e.g., more than 1, less than 5) words of each other. The goal is to search over a prose text, and find unneeded repetitions of words close…
1
2 3
10 11