4 Replies - 1569 Views - Last Post: 15 December 2012 - 04:32 AM Rate Topic: -----

#1 huzi8t9  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 326
  • Joined: 11-July 07

[SOLVED] RegEx help - should be simple...

Posted 14 December 2012 - 04:17 PM

Hey, D.I.C.

I'm having a little more trouble with my RegEx, although I can't figure out what I'm doing wrong, regardless of much research.

Here's what's meant to happen:
My script is given a string (which starts with either (who|what|where|why|when) which would suggest that the string is a question. Then, I'm figuring out of it is past or present tense (is|was) then I want to get everything else as a whole string after than.

That proved difficult since I was using the ([\w]+) and that wouldn't do the trick. So I proceeded with this method, used array_slice to chop off the first two elements. Surely, this would be a success? No.

My RegEx is only matching the first three words, so when I use Who is the president of America? - my array of matches appears to be Array([0]=>"Who is the" [1]=>"who" [2]=>"is" [3]=>"the"). I've tried amending my code in many ways, shapes and forms to try and get some order by this script is running riot and not wanting to help me out at all.

Below is my function to break down the sentence/string - hopefully, someone will be able to give me some explanation.

function isQuestion($str) {
    $q = preg_match("/(who|what|where|why|when) (is|was) ([\w]+)/i", $str, $matches);
    return Array($q, $matches);
}



Many thanks in advanced, always appreciated.

~huzi

Solution (thanks to Dormilich)
function isQuestion($str) {
    $q = preg_match("/(who|what|where|why|when) (is|was) ([\w].+)/i", $str, $matches);
    return Array($q, $matches);
}


This post has been edited by huzi8t9: 14 December 2012 - 04:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: [SOLVED] RegEx help - should be simple...

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,251
  • Joined: 08-June 10

Re: [SOLVED] RegEx help - should be simple...

Posted 14 December 2012 - 04:30 PM

View Posthuzi8t9, on 15 December 2012 - 12:17 AM, said:

My RegEx is only matching the first three words,

because you tell it to do exactly that. ([\w]+) (of which the [] are superfluous, btw.) is capturing all word characters (letters, digits & _) until the first non-word character (usually a space or punctuation). after that you donít tell it to proceed. (.+)should be sufficient (unless you have control characters like line breaks in it).

PS. what about exploding the string on the space character?
Was This Post Helpful? 2
  • +
  • -

#3 huzi8t9  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 326
  • Joined: 11-July 07

Re: [SOLVED] RegEx help - should be simple...

Posted 14 December 2012 - 04:34 PM

You're a life saver, Dormilich, you really are! Thank you.

As for the PS, I'm controlling the rest of the sentence after the first two words have been processed - now it works beautifully, thanks to you!

Thank you very much! If I could give more +'s, I would!

~huzi

This post has been edited by Dormilich: 14 December 2012 - 04:35 PM
Reason for edit:: removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,251
  • Joined: 08-June 10

Re: [SOLVED] RegEx help - should be simple...

Posted 14 December 2012 - 04:36 PM

View Posthuzi8t9, on 15 December 2012 - 12:34 AM, said:

You're a life saver

I have learnt to read code like a parser (i.e. verbatim), thatís the whole magic.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,251
  • Joined: 08-June 10

Re: [SOLVED] RegEx help - should be simple...

Posted 15 December 2012 - 04:32 AM

View Posthuzi8t9, on 15 December 2012 - 12:17 AM, said:

Solution (thanks to Dormilich)
function isQuestion($str) {
    $q = preg_match("/(who|what|where|why|when) (is|was) ([\w].+)/i", $str, $matches);
    return Array($q, $matches);
}


omit the [\w]. first, \w is already a character class, second, \w is already contained in ..

PS. alternate idea for the first part of the RegExp: (wh(?:​o|at|ere|y|en))

PPS. don’t copy that text, it contains a zero-width space to prevent emoticons.

This post has been edited by Dormilich: 15 December 2012 - 04:34 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1