Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Not sure if this is possible.


    I want to use regex to find three words, now two of those words absolutely match, but a variation of one and the other two must exist.

    I then need to take the last bit of that string if regex can find a specific variation

    this is the text.

    This is my regex to find the three words.

    Code:
    \b(hum|([yY]ou|(your)?))\b
    
    You continue to hum your song.
    You continue to fumble slightly as you hum a confident lullaby.
    however if the second string shows, I need to use regex to grab the last two words only if you and hum are present.

    I am only able to use regex so I hope this is possible. Can anyone help me out?
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,055
    Rep Power
    9398
    Your description is confusing.

    Given each of those strings,
    1. What parts are "special"?
    2. What parts are you trying to extract?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0
    Hum will be in a string with You and Your. I need it to only be true if Hum and Your or Hum and You are in the same sentence.

    If Hum and Your are in the sentence then I need it to grab the last two words of that sentence as well. ALL using regex only
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0
    Is this possible? I am sure it is, I got pretty close by altering the regex expression above. I just can't get it to return nothing if Hum is present when You or Your.

    Is this making sense yet?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0
    Ok I figured out how to get the first part


    Code:
    (\b[yY]our?\b.*)(\bhumm?i?n?g?\b\s?(\byour\b.*)?)
    This works on the following three sentences.

    Code:
    You continue to hum your song.
    
    You continue to fumble slightly as you hum a confident lullaby.
    
    You finish humming a confident lullaby.
    Now I just need to capture Confident Lullaby if the previous regex matches.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0
    Originally Posted by krptodr
    Ok I figured out how to get the first part


    Code:
    (\b[yY]our?\b.*)(\bhumm?i?n?g?\b\s?(\byour\b.*)?)
    This works on the following three sentences.

    Code:
    You continue to hum your song.
    
    You continue to fumble slightly as you hum a confident lullaby.
    
    You finish humming a confident lullaby.
    Now I just need to capture Confident Lullaby if the previous regex matches.
    Well I thought I had all checks. If the first sentence has the first Y removed in You then it returns a complete false on all groups. This is because it is not set as an optional word. However if it is set as an optional word, then it returns true because of the word your. But your is also an optional word and if y in your is removed then it returns true matching just hum. How can I do a check against this?
  12. #7
  13. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,055
    Rep Power
    9398
    So the sentence must include "hum" and "you", or "hum" and "your", and in the latter case you want the last two words.
    Code:
    \bhum\b.*?\b(?:(you)\b|(your)\b.*\s(\S+\s+\S+))|\b(?:(you)\b.*?\bhum\b|(your)\b.*\bhum\b.*\s(\S+\s+\S+))
    The pronoun is "$1$2$4$5" and the last two words are "$3$6".

    However that goes according to exactly what you said, and I'm pretty sure you aren't being as precise as you could be. It probably has too many \bs too.
    The string is a sentence that should contain "hum your" or "you * hum". In the latter case, I need the last two words of the sentence.
    Code:
    (\bhum your\b)|(\byou\b.*?\bhum).*\s(\S+\s+\S+)
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0
    Originally Posted by requinix
    So the sentence must include "hum" and "you", or "hum" and "your", and in the latter case you want the last two words.
    Code:
    \bhum\b.*?\b(?:(you)\b|(your)\b.*\s(\S+\s+\S+))|\b(?:(you)\b.*?\bhum\b|(your)\b.*\bhum\b.*\s(\S+\s+\S+))
    The pronoun is "$1$2$4$5" and the last two words are "$3$6".

    However that goes according to exactly what you said, and I'm pretty sure you aren't being as precise as you could be. It probably has too many \bs too.

    Code:
    (\bhum your\b)|(\byou\b.*?\bhum).*\s(\S+\s+\S+)
    Wow, I will have to give this a try. I am new to regex, so I was shocked to atleast get the expression I created lol
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0
    This works, but not entirely. The last sentence isn't being matched at all. And the first last two words aren't the only words being matched
  18. #10
  19. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,055
    Rep Power
    9398
    Originally Posted by krptodr
    The last sentence isn't being matched at all.
    Are you applying it case-insensitively?

    Originally Posted by krptodr
    And the first last two words aren't the only words being matched
    What's the code you have using this regular expression?
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0
    Originally Posted by requinix
    Are you applying it case-insensitively?
    I am applying it case sensitive

    Originally Posted by requinix
    What's the code you have using this regular expression?
    I dont have any code for it, im just trying to use regex to match exactlly what I reuqest... however not all regex engines are the same... so I should inform you that im testing my expressions with RegExr which uses actionscript... atleast I believe it does.
  22. #12
  23. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,055
    Rep Power
    9398
    Originally Posted by krptodr
    I am applying it case sensitive
    Then don't.

    Originally Posted by krptodr
    I dont have any code for it, im just trying to use regex to match exactlly what I reuqest... however not all regex engines are the same... so I should inform you that im testing my expressions with RegExr which uses actionscript... atleast I believe it does.
    So you do recognize that regex engines are different, but you continue to use one to test it? Are you ultimately going to be using ActionScript?
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0
    No I am going to use the regex expression in a program that was designed in .net to implement regex.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0
    Ok, so I tried your expression in a .Net regex tool, and came up with the same results......

    http://regexhero.net/tester/

    It appears actionscript and .net engines of regex are similar.
  28. #15
  29. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,055
    Rep Power
    9398
    I tried my expression in that tool and got the right results.

    Are you using the second one I posted? I hacked out the first one with the intention that it looked complicated, not work.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo