Author Topic: Regex (regular expressions)  (Read 6158 times)

0 Members and 1 Guest are viewing this topic.

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Regex (regular expressions)
« 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]
@"""[^>]+""|""[^>]+|""[^>]+'->'"

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: Regex (regular expressions)
« Reply #1 on: October 23, 2011, 03:31:09 pm »
Try this?
Code: [Select]
\"(.+?)(\"|$|->)
I tested it here: 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 (see the first answer).
« Last Edit: October 23, 2011, 03:40:23 pm by Michael_Lee »
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: Regex (regular expressions)
« Reply #2 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!

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: Regex (regular expressions)
« Reply #3 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.
« Last Edit: October 23, 2011, 03:41:45 pm by Michael_Lee »
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: Regex (regular expressions)
« Reply #4 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.

Offline C0deH4cker

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 258
  • Rating: +11/-1
    • View Profile
    • iNinjas Forum/Repo
Re: Regex (regular expressions)
« Reply #5 on: October 23, 2011, 04:54:00 pm »
can anyone point me to a really good regex tutorial that you KNOW is good?

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Regex (regular expressions)
« Reply #6 on: October 23, 2011, 04:54:59 pm »
http://regular-expressions.info/

Everything is there, complete with examples.

Offline C0deH4cker

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 258
  • Rating: +11/-1
    • View Profile
    • iNinjas Forum/Repo
Re: Regex (regular expressions)
« Reply #7 on: October 23, 2011, 04:57:03 pm »
alright, thanks! +1

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: Regex (regular expressions)
« Reply #8 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...
« Last Edit: April 05, 2012, 12:01:17 pm by BlakPilar »

Offline cooliojazz

  • Support Staff
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 619
  • Rating: +66/-9
  • I omnoms on your soul
    • View Profile
    • Unreal Phantasies
Re: Regex (regular expressions)
« Reply #9 on: April 05, 2012, 04:47:05 pm »
How about /'([^\\]|\\.)'/ does that do what you want?
« Last Edit: April 05, 2012, 04:47:17 pm by cooliojazz »
Spoiler For Random signess:
You can not beat my skills.
Trust me.
So don't even try.
And remember never to trust someone who says, "Trust me."

TI File Editor Progress: Remade in java like a boss. 50% we'll call it? IDK =P
Java Libraries: JIRC - 90% JTIF - 5%
TI Projects: Unreal Notator - -5000%
Nomcraft, a Bukkit mod
Some of the music I write can be found here | The Rest Should Be Here (Bandcamp)

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: Regex (regular expressions)
« Reply #10 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!
« Last Edit: April 05, 2012, 06:03:42 pm by BlakPilar »

Offline cooliojazz

  • Support Staff
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 619
  • Rating: +66/-9
  • I omnoms on your soul
    • View Profile
    • Unreal Phantasies
Re: Regex (regular expressions)
« Reply #11 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?
« Last Edit: April 05, 2012, 06:20:13 pm by cooliojazz »
Spoiler For Random signess:
You can not beat my skills.
Trust me.
So don't even try.
And remember never to trust someone who says, "Trust me."

TI File Editor Progress: Remade in java like a boss. 50% we'll call it? IDK =P
Java Libraries: JIRC - 90% JTIF - 5%
TI Projects: Unreal Notator - -5000%
Nomcraft, a Bukkit mod
Some of the music I write can be found here | The Rest Should Be Here (Bandcamp)