Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 47
  1. #16
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    I notice you using [^.] instead of [^\.\?\!] so I changed that:
    [code]
    /(?:^|\.|\?|\!)?[\w]([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)/

    Different results, but still not right.

    Also it would seem that (?:^|\.|\?|\!)? is doing nothing at all.

    I removed it completely, to get this:
    /[\w]([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)/
    and got the same (wrong) results.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  2. #17
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    I will say that your (?:^|\.|\?|\!) seemed to have mostly worked. Surprised me.
    Yes, I forgot a | in this expression. ^ means the beginning of the line when it is outside of a character class.

    So this is a non-capturing group that will be either the beginning of a line (or the string), a full-stop, etc..

    But, no, I'm not testing it as thoroughly as you are.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  3. #18
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Well this works with the expressions xelawho proposed, but only if it starts with a Capital letter (the original request):

    Code:
    (?:^|\.|\;|\!|\?)\s?([A-Z][^.]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
    but again I'm not testing it beyond the sample phrases, so I won't be offended if it is dropped

    Revision:

    Code:
    (?:^|\.|\;|\!|\?)\s?([A-Z][^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
    Attached Thumbnails Attached Thumbnails another regex question-regex2.png  
    Last edited by AndrewGSW; 01-31-2013 at 10:49 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  4. #19
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    I notice you using [^.] instead of [^\.\?\!] so I changed that
    @Old Pedant - yes, the ! and ? weren't mentioned until recently
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  5. #20
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    216
    Thanks
    0
    Thanked 29 Times in 27 Posts
    These two pages could maybe inspire a solution : Highlight a word and Select a Sentence

  6. #21
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by AndrewGSW View Post
    Yes, I forgot a | in this expression. ^ means the beginning of the line when it is outside of a character class.
    I knew that. DOH. I was just completely misreading the intent of that sub-expression. Double DOH on me.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  7. #22
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,763
    Thanks
    55
    Thanked 517 Times in 514 Posts
    looks to me like
    Code:
    (?:^|\.|\?|\!)?[\w]([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
    and
    Code:
    [\w]([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
    are the only one that fulfill the 4 requirements

    (the rule is not that it must begin with a capital letter, but that if there is no capital letter preceding the keyword, it captures the string from the start)

    thanks everybody!

  8. #23
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by xelawho View Post
    (the rule is not that it must begin with a capital letter, but that if there is no capital letter preceding the keyword, it captures the string from the start)
    And what about a period that is *NOT* followed by a capital letter?

    aardvarks whistle. dogs jump when they are happy

    Given your rule as stated here, that would return the *entire* string.

    Is that truly what you want?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  9. #24
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    I suppose my version without requiring a capital would be

    Code:
    (?:^|\.|\;|\!|\?)\s?([\w][^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
    but all my sentences begin with capital letters (sic.) and end with a smile

    Just for completeness (for anyone reading) to embed a string in a regex requires use of the RegExp constructor:

    Code:
    var re = new RegExp("^\\/" + resource + "\\/\\d+$");
    Last edited by AndrewGSW; 01-31-2013 at 11:49 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  10. #25
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by xelawho View Post
    looks to me like
    Code:
    (?:^|\.|\?|\!)?[\w]([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
    and
    Code:
    [\w]([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
    are the only one that fulfill the 4 requirements

    (the rule is not that it must begin with a capital letter, but that if there is no capital letter preceding the keyword, it captures the string from the start)

    thanks everybody!
    Don't these versions drop the first letter?

    Added: I think I would account for zero or more spaces at the beginning as well:

    Code:
    (?:^|\.|\;|\!|\?)\s*([\w][^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
    and also if happy is the very first word:
    Code:
    (?:^|\.|\;|\!|\?)\s*([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
    Last edited by AndrewGSW; 01-31-2013 at 11:58 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  11. #26
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,763
    Thanks
    55
    Thanked 517 Times in 514 Posts
    no - they both capture the full stop at the end of the preceding sentence.

  12. #27
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,763
    Thanks
    55
    Thanked 517 Times in 514 Posts
    Quote Originally Posted by Old Pedant View Post
    And what about a period that is *NOT* followed by a capital letter?

    aardvarks whistle. dogs jump when they are happy

    Given your rule as stated here, that would return the *entire* string.

    Is that truly what you want?
    Yes. This is to do spellchecks on content editable divs on a website for professional writers. If they're starting their sentences with lower case letters they have bigger problems than a few extra characters in their output.

    maybe a little detail will help. The spellchecker scans the text nodes, so if the innerHTML is
    Code:
    I had a <b>great</b> day
    it will scan 3 chunks:
    1) I had a
    2) great
    3) day

    Which is kind of meaningless there, but there could be an entire paragraph without formatting and so you just want to show a snippet. A sentence would be ideal, but by the time the spellchecker gets to the text it's all broken up into text nodes. So instead of piecing it back together, there are the four rules (rule 3 revised halfway through this thread):

    - Start capturing from the closest word before the variable word that starts with a capital/uppercase.
    - If there is no word that starts with a capital before the variable word, start capturing from the start of the string.
    - Equally, if the part of the string after the variable word contains a full stop, exclamation mark or question mark, finish capturing at the punctuation.
    - If not, capture until the end of the string.

    Make sense?

  13. #28
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Well, I gave you my answer, using 4 regexps. Which I see no reason can't be combined into one. And then one more regexp eliminates not only any preceding [.?!] but also trims the string on the left side of any spaces.

    Unless you are a fanatic about doing it all with a single regexp, I don't see what is wrong with that. What difference does it make if it's 1 regexp or 2 or 5?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  14. #29
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,763
    Thanks
    55
    Thanked 517 Times in 514 Posts
    but you're right Andrew - neither of the ones that I thought were winners in #22 capture anything if the word comes at the start of the string

  15. #30
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by xelawho View Post
    but you're right Andrew - neither of the ones that I thought were winners in #22 capture anything if the word comes at the start of the string
    I haven't tested my most recent solution in great depth but I'll repeat it here:
    and also if happy is the very first word:
    Code:
    (?:^|\.|\;|\!|\?)\s*([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
    All those escapes \, though, are not necessary in a character class - Added: I'm blaming OP for this (I wouldn't have included them )

    Code:
    (?:^|\.|\;|\!|\?)\s*([^.!?]*happy[^.!?]*)(?:\.|\?|\!|$)
    Last edited by AndrewGSW; 02-01-2013 at 01:34 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


 
Page 2 of 4 FirstFirst 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •