Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: BlakPilar on October 23, 2011, 02:19:00 pm
-
Does anyone know regex well? I need to be able to select everything between a double quote and either another double quote, the end of a line, or a two character sequence ("->" for example). What I have now kind of works, except the highlighting only stops after the '>' and sometimes it selects everything for a while after the '>'.
Here's what it is now: (by the way, for the '->' part, I completely guessed lol)
@"""[^>]+""|""[^>]+|""[^>]+'->'"
-
Try this?
\"(.+?)(\"|$|->)
I tested it here: http://regexpal.com/ (http://regexpal.com/)
but I'm not certain if there are any syntax differences between that and C# (which is what I think you're using?)
If you want only what's between the quotation marks/->/newline, capture only the first matching group (http://stackoverflow.com/questions/1454913/regular-expression-to-find-a-string-included-between-two-characters-while-exclu) (see the first answer).
-
Ahh, yes, that works. The only thing is it highlights '->' too... Oh well, good enough for now! Thank you!
-
No problem.
Have you tried looking at the stackoverflow link and tried capturing the first matching group instead of capturing the entire regex? That might solve the highlighted '->' problem.
-
Oops, didn't see that part lol. I'll look at it and see what I can do, thanks again.
-
can anyone point me to a really good regex tutorial that you KNOW is good?
-
http://regular-expressions.info/
Everything is there, complete with examples.
-
alright, thanks! +1
-
So, I have another regex question... I looked at the tutorial and reference guide, but I can't seem to find out how to do what I want.
I want to be able to match everything between two single quotes ('...') but only if there is a single character between them, or a backslash then another character. Basically, C-style character declarations. This is what I have right now: \'.+?\'. It doesn't capture '' which is what I wanted, but it captures anything that contains one or more character between the single quotes (I know it's because of the +'s "rule"). I mean, I suppose I could do something like \'[a-zA-Z0-9 _\?!@#$%^&\*\(\)\[\]]\' but that would be cheating and wouldn't capture everything...
-
How about /'([^\\]|\\.)'/ does that do what you want?
-
I'm using C# (so .NET) and I had to change that to \'([^\\]|\\.)\', but it says there's an unterminated [] pair, which is weird... I tried changing it to \'(.|\\.)\' (I don't understand the need for a character class :/). My test search string is 'gh' ' ' 'j' ';' ' fsdf' '' and when I did the changed version my result was ' ', ' ', ' ', ' '
EDIT: Wow, nevermind... I added an @ before the edited regex thing and changed my string so there were commas between the character examples and it worked fine. Thank you!
-
The character class is so it will capture \ characters, without it, it will return just \ if you say have '\a' instead of what it should, which is \a. The problem is that you probably didn't re-escape everything... =P if you wanted to actually put it in a string it would need to be "\'([^\\\\]|\\\\.)\'". (Do you really need to escape single quotes in strings defined with double quotes?)
EDIT, also I'm curious, what does the @ do?