Lets say I have this string:     key something.key() (.key)(key)   I would like regex to only match the word "keys" that are inside "[ ]"     [key] something.key() (.key)([key])   I have used this regex currently /(?!\.)key/g  But that only excludes dot and still selects word even if it startet with a dot.
3909
Is there a way to achieve the equivalent of a  in javascript regular expressions?  I need to match a string that does not start with a specific set of characters.  It seems I am unable to find a regex that does this without failing if the matched part is found at the beginning of the string.  Negative lookbehinds seem to be the only answer, but javascript doesn't have one.  EDIT: This is the regex that I would like to work, but it doesn't:  (?&lt;!([abcdefg]))m  So it would match the 'm' in 'jim' or 'm', but not 'jam'
51943
Is it possible to skip a couple of characters in a capture group in regular expressions? I am using .NET regexes but that shouldn't matter.  Basically, what I am looking for is:     [random text]AB-123[random text]   and I need to capture 'AB123', without the hyphen.  I know that AB is 2 or 3 uppercase characters and 123 is 2 or 3 digits, but that's not the hard part. The hard part (at least for me) is skipping the hyphen.  I guess I could capture both separately and then concatenate them in code, but I wish I had a more elegant, regex-only solution.  Any suggestions?
115440
Simply said, I just can't find a way to match a variable against some pattern containing spaces.  Here is what I expected to work (echo 'ok' string)  item='foobar baz' pat=".+bar baz" if [[ "$item" =~ "$pat" ]] ; then     echo ok fi   Adding/removing quotes around $pat does not seem to make any difference. I get those two erros below:  bash: conditional binary operator expected bash: syntax error near `~='   Could someone please help me pointing out what I am doing wrong here ? Should I put the pattern right away (without any quotes/double quotes, nor variable reference) ? If that is the case, then how can I put spaces in ? (using reg-exp matching, not an alternative)  Thank you !
50593
I am looking for a regex that will find repeating letters. So any letter twice or more, for example:  booooooot or abbott   I won't know the letter I am looking for ahead of time.   This is a question I was asked in interviews and then asked in interviews. Not so many people get it correct.
41680
If I have the following string:  string s = "abcdefghab";   Then how do I get a string (or char[]) that has just the characters that are repeated in the original string using C# and LINQ. In my example I want to end up with "ab".  Although not necessary, I was trying to do this in a single line of LINQ and had so far come up with:  s.ToCharArray().OrderBy(a =&gt; a)...
591
I am tired of always trying to guess, if I should escape special characters like '()[]{}|' etc. when using many implementations of regexps.  It is different with, for example, Python, sed, grep, awk, Perl, rename, Apache, find and so on. Is there any rule set which tells when I should, and when I should not, escape special characters? Does it depend on the regexp type, like PCRE, POSIX or extended regexps?
60761
I'm looking for the string foo= in text files in a directory tree. It's on a common Linux machine, I have bash shell: grep -ircl &quot;foo=&quot; *  In the directories are also many binary files which match &quot;foo=&quot;. As these results are not relevant and slow down the search, I want grep to skip searching these files (mostly JPEG and PNG images). How would I do that? I know there are the --exclude=PATTERN and --include=PATTERN options, but what is the pattern format? The man page of grep says: --include=PATTERN     Recurse in directories only searching file matching PATTERN. --exclude=PATTERN     Recurse in directories skip file matching PATTERN.  Searching on grep include, grep include exclude, grep exclude and variants did not find anything relevant If there's a better way of grepping only in certain files, I'm all for it; moving the offending files is not an option. I can't search only certain directories (the directory structure is a big mess, with everything everywhere). Also, I can't install anything, so I have to do with common tools (like grep or the suggested find).
2940
How can I test if a RegEx matches a string exactly?  var r = /a/; r.test("a"); // returns true r.test("ba"); // returns true testExact(r, "ba"); // should return false testExact(r, "a"); // should return true
3211
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM J0000010: Project name: E:\foo.pf J0000011: Job name: MBiek Direct Mail Test J0000020: Document 1 - Completed successfully  I'm trying to extract pieces from it using regex. In this case, I want to grab everything after Project Name up to the part where it says J0000011: (the 11 is going to be a different number every time). Here's the regex I've been playing with: Project name:\s+(.*)\s+J[0-9]{7}:  The problem is that it doesn't stop until it hits the J0000020: at the end. How do I make the regex stop at the first occurrence of J[0-9]{7}?
62677
I am writing a Java utility which helps me to generate loads of data for performance testing.  It would be really cool to be able to specify a regex for Strings so that my generator spits out things which match this.  Is there something out there already baked which I can use to do this?  Or is there a library which gets me most of the way there?  Thanks
116235
Trying to write -regex for a find statement that shows only directories with dots and numbers, for example 1.2.3.4 oraz 1.2.3 or 1.3.4.5.6 was trying something like  "-regex '\.[0-9.]+'", but it also shows dirs with letters. And I only want directories with numbers and dots.
47023
Say I have the string "User Name:firstname.surname" contained in a larger string how can I use a regular expression to just get the firstname.surname part?  Every method i have tried returns the string "User Name:firstname.surname" then I have to do a string replace on "User Name:" to an empty string.  Could back references be of use here?  Edit:  The longer string could contain "Account Name: firstname.surname" hence why I want to match the "User Name:" part of the string aswell to just get that value.
18905
I've got a text file full of records where each field in each record is a fixed width.  My first approach would be to parse each record simply using string.Substring().  Is there a better way?   For example, the format could be described as:  &lt;Field1(8)&gt;&lt;Field2(16)&gt;&lt;Field3(12)&gt;   And an example file with two records could look like:  SomeData0000000000123456SomeMoreData Data2   0000000000555555MoreData       I just want to make sure I'm not overlooking a more elegant way than Substring().    Update:  I ultimately went with a regex like Killersponge suggested:  private readonly Regex reLot = new Regex(REGEX_LOT, RegexOptions.Compiled); const string REGEX_LOT = "^(?&lt;Field1&gt;.{6})" +                         "(?&lt;Field2&gt;.{16})" +                         "(?&lt;Field3&gt;.{12})";   I then use the following to access the fields:  Match match = reLot.Match(record); string field1 = match.Groups["Field1"].Value;
5915
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 regular expression?   Input:  hoho hihi haha hede   Code:  grep "&lt;Regex for 'doesn't contain hede'&gt;" input   Desired output:  hoho hihi haha
22511
I want to use input from a user as a regex pattern for a search over some text.  It works, but how I can handle cases where user puts characters that have meaning in regex? For example, the user wants to search for Word (s): regex engine will take the (s) as a group.  I want it to treat it like a string &quot;(s)&quot; .  I can run replace on user input and replace the ( with \( and the ) with \) but the problem is I will need to do replace for every possible regex symbol. Do you know some better way ?
33382
Can a regular expression match whitespace or the start of a string?  I'm trying to replace currency the abbreviation GBP with a £ symbol. I could just match anything starting GBP, but I'd like to be a bit more conservative, and look for certain delimiters around it.  &gt;&gt;&gt; import re &gt;&gt;&gt; text = u'GBP 5 Off when you spend GBP75.00'  &gt;&gt;&gt; re.sub(ur'GBP([\W\d])', ur'£\g&lt;1&gt;', text) # matches GBP with any prefix u'\xa3 5 Off when you spend \xa375.00'  &gt;&gt;&gt; re.sub(ur'^GBP([\W\d])', ur'£\g&lt;1&gt;', text) # matches at start only u'\xa3 5 Off when you spend GBP75.00'  &gt;&gt;&gt; re.sub(ur'(\W)GBP([\W\d])', ur'\g&lt;1&gt;£\g&lt;2&gt;', text) # matches whitespace prefix only u'GBP 5 Off when you spend \xa375.00'   Can I do both of the latter examples at the same time?
3542
I have a value like this:  "Foo Bar" "Another Value" something else   What regex will return the values enclosed in the quotation marks (e.g. Foo Bar and Another Value)?