Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: BlakPilar on October 23, 2011, 02:19:00 pm

Title: Regex (regular expressions)
Post 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)
Code: [Select]
@"""[^>]+""|""[^>]+|""[^>]+'->'"
Title: Re: Regex (regular expressions)
Post by: Michael_Lee on October 23, 2011, 03:31:09 pm
Try this?
Code: [Select]
\"(.+?)(\"|$|->)
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).
Title: Re: Regex (regular expressions)
Post by: BlakPilar on October 23, 2011, 03:39:52 pm
Ahh, yes, that works. The only thing is it highlights '->' too... Oh well, good enough for now! Thank you!
Title: Re: Regex (regular expressions)
Post by: Michael_Lee on October 23, 2011, 03:41:24 pm
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.
Title: Re: Regex (regular expressions)
Post by: BlakPilar on October 23, 2011, 03:51:12 pm
Oops, didn't see that part lol. I'll look at it and see what I can do, thanks again.
Title: Re: Regex (regular expressions)
Post by: C0deH4cker on October 23, 2011, 04:54:00 pm
can anyone point me to a really good regex tutorial that you KNOW is good?
Title: Re: Regex (regular expressions)
Post by: Deep Toaster on October 23, 2011, 04:54:59 pm
http://regular-expressions.info/

Everything is there, complete with examples.
Title: Re: Regex (regular expressions)
Post by: C0deH4cker on October 23, 2011, 04:57:03 pm
alright, thanks! +1
Title: Re: Regex (regular expressions)
Post by: BlakPilar on April 05, 2012, 12:00:44 pm
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...
Title: Re: Regex (regular expressions)
Post by: cooliojazz on April 05, 2012, 04:47:05 pm
How about /'([^\\]|\\.)'/ does that do what you want?
Title: Re: Regex (regular expressions)
Post by: BlakPilar on April 05, 2012, 05:58:12 pm
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!
Title: Re: Regex (regular expressions)
Post by: cooliojazz on April 05, 2012, 06:16:36 pm
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?