tags:

views:

219

answers:

8

Alright, i promised myself i would learn Regex one day.. but today is not that day.

What is the correct expression for matching #_ (where _ is any character EXCEPT {)?


Clarification:

I am working on a syntax highlighting system for Ruby and i am defining the rules for comments. The specification that the '{' not be included is to differentiate a comment from variables embedded in strings.

+4  A: 

In PCRE syntax, #[^{]

David Zaslavsky
+10  A: 

You define a character class inside [ ] and negate with ^:

[^{] // i.e. a single character that is not {

If you add the hash mark in the beginning this results in:

#[^{] // i.e. # and then a single character that is not {
Fabian Steeg
at least there is an explanation.
sfossen
A: 

^#[^{]$

That's my answer if you meant to just match two characters.

Tom
that means a line consisting only of those 2 characters.
sfossen
isn't that what he is asking for?
Tom
+1, that's a valid literal interpretation of the question as stated.
Adam Bellaire
"You are technically correct, the BEST KIND of correct!" It is a correct but useless answer.
Schwern
+3  A: 

This feels like a CS 101 homework question to me.

I suspect the right answer is to actually learn regular expressions today. What are you trying to match and why?

slacy
A: 

Rework is a tool that will help you interactively explore regular expressions.

lucas-insasho
A: 

Rubular is another great website for testing regular expressions.

Ryan Bigg
A: 

If you're trying to do syntax highlighting, do yourself a favor and don't try to hack something together with regular expressions. You'll wind up piling hack on top of hack on top of hack until it's a scattered mess of almost-works and good-enoughs. Trust me, you'll hate yourself.

Go directly to using a grammar. Either by putting one straight out of the source code or finding an existing grammar on the Internet.

Schwern
A: 

This site is great for help: txt2re

May not help with your problem but will in the future.

Sean