Author Topic: Regex question  (Read 6015 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Regex question
« on: January 01, 2012, 03:24:44 pm »
I have a question. How can I make a regex that searches for an entire string? I've used regex's to search for symbols and specific stuff like email addresses and whitespace.

However, I just faced a new issue. How can I use a regex to search for an entire string like "omnimaga.org"? Thanks.

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 question
« Reply #1 on: January 01, 2012, 03:47:22 pm »
Just search for "omnimaga.org." If you're using PCRE, /omnimaga\.org/ works nicely.

Remember that except for metacharacters (the characters that actually "mean something" in regex), regular expressions are just strings.
« Last Edit: January 01, 2012, 03:48:02 pm by Deep Thought »




Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Regex question
« Reply #2 on: January 02, 2012, 06:08:13 am »
I am using PCRE indeed. I tried "\\mystring/", but it didn't really work. Any idea? Thanks.

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Regex question
« Reply #3 on: January 02, 2012, 06:13:25 am »
Is "\\mystring/" the literal you typed in?  That will search for "\mystring/".  If you are specifically trying to regex out websites, you can try something like "www(\..+)*" for instance.
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Regex question
« Reply #4 on: January 02, 2012, 06:15:25 am »
Is "\\mystring/" the literal you typed in?  That will search for "\mystring/".  If you are specifically trying to regex out websites, you can try something like "www(\..+)*" for instance.

No, it's not a website, the "omnimaga.org" was a bad example, let's try "omnimaga". I would have "\\omnimaga/" as the literal string, so that the \\ turns to a \. Is that alright to look for omnimaga? Thanks.

Code: [Select]
{this is a pattern}
I also found online patterns in PCRE should be like that, but it's not working either.
« Last Edit: January 02, 2012, 06:30:17 am by ephan »

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 question
« Reply #5 on: January 02, 2012, 11:25:03 am »
If you're searching for "omnimaga," just search for "omnimaga." For example, preg_match_all('/omnimaga/', 'http://www.omnimaga.org/') matches the "omnimaga" in "http://www.omnimaga.org/." (The slashes around the first string are the regex delimiters, which are required.)

I suggest you go over http://regular-expressions.info/ for more information on regex.
« Last Edit: January 02, 2012, 11:26:01 am by Deep Thought »




Offline JL235

  • LV3 Member (Next: 100)
  • ***
  • Posts: 57
  • Rating: +13/-0
    • View Profile
    • Play My Code
Re: Regex question
« Reply #6 on: January 07, 2012, 02:44:05 pm »
I'm still not sure if your after 'omega' or '"omega"' (do you want to include the quotes?). If it's just 'omega', then the regex is just 'omega'. That means your looking for an o, followed by an m, followed by an e, followed by a g and then an a.

In some languages you then have to format it in a special way, such as in JavaScript you add a / on either side, so it becomes /omega/. In PHP you do the same, and then put it inside a string, so it becomes "/omega/". Just be aware that the / wrapping is a convention used by languages to state that it's a regex, and not the regex it's self. As a result different language have different rules.

If you want to use special characters, such as [, ., ^, /, \, $, * or +; then your right that you append a \ before it. However in some languages the \ already means something, such as "\n" is a new line in Java and PHP. So in those languages you have to escape the slash. For example the regex '\.omega' would become '\\.omega' in Java and (I think) PHP.

If you state what language your working in, then more accurate help could be provided. There are also plenty of online regex testers available online, such as this one here.
Build games in the browser at PlayMyCode.com, @playmycode

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: Regex question
« Reply #7 on: January 07, 2012, 05:19:32 pm »
Actually, JL235, if you're trying to match the string "\.omega" then you'd have to have a pattern of "\\\.omega" because you have to escape both the \ and the . in the pattern.
There's something about Tuesday...


Pushpins 'n' stuff...


Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Regex question
« Reply #8 on: January 07, 2012, 05:36:25 pm »
I'm working in Vala. I realized saying that was a bit useless because I said I'm using PCRE. Isn't PCRE what defines how it works?

I think in Vala I just need the "omega" by the way.

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Regex question
« Reply #9 on: January 07, 2012, 05:41:03 pm »
Just a little note, regexp are never in quotes :D
I made this mistake many times :P

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: Regex question
« Reply #10 on: January 07, 2012, 05:48:26 pm »
Jim, RegEx patterns are placed in strings in Java. They are not wrapped with //.
There's something about Tuesday...


Pushpins 'n' stuff...


Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Regex question
« Reply #11 on: January 07, 2012, 05:49:35 pm »
Oh, but he is using vala (based on c)

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: Regex question
« Reply #12 on: January 07, 2012, 06:01:54 pm »
I am aware. Your comment sounded like a general statement about RegEx to me, in which case it is false.
There's something about Tuesday...


Pushpins 'n' stuff...


Offline JL235

  • LV3 Member (Next: 100)
  • ***
  • Posts: 57
  • Rating: +13/-0
    • View Profile
    • Play My Code
Re: Regex question
« Reply #13 on: January 08, 2012, 05:15:08 am »
My bad if it's not quite right, but my general point is that it's normally a two stage process. Working out the regex, and then working out how to get it working in the language. That second can trip you up sometimes.
Build games in the browser at PlayMyCode.com, @playmycode

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Regex question
« Reply #14 on: January 08, 2012, 10:12:24 am »
I am aware. Your comment sounded like a general statement about RegEx to me, in which case it is false.
I'm sorry, should have mentions the RegExp type.
Indeed you had a valid statement.