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 3 of 4 FirstFirst 1234 LastLast
Results 31 to 45 of 47
  1. #31
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    The more you look at these things the more they can be simplified

    Code:
    (?:^|[.;!?])\s*([^.!?]*happy[^.!?]*)(?:[.?!]|$)
    "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

  2. #32
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,984
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I just can't believe it's so complicated it needs 4 regexes...

    isn't it possible just to make this:
    Code:
    (?:^|\.|\;|\!|\?)\s*([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
    start capturing at the first capital (and what's with the sic on capital?) instead of the full stop of the previous sentence?

  3. #33
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,984
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Quote Originally Posted by AndrewGSW View Post
    The more you look at these things the more they can be simplified

    Code:
    (?:^|[.;!?])\s*([^.!?]*happy[^.!?]*)(?:[.?!]|$)
    so close...

    I just need to get rid of that leading full`stop and space before the capital (if there is one)

  4. #34
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,719
    Thanks
    80
    Thanked 4,514 Times in 4,478 Posts
    Your conditions are self contradictory, that's why it's hard to do it all in one RegExp.

    "If a capital letter is not found (going backwards from the word to be found), then look for a [.?!]. If a [.?!] is not found look for the beginning of the text."

    So the search for punctuation can only take place *IF* the search for a capital letter fails.
    And the search for beginning of text can only take place *IF* both the other searches failed.

    On top of that, you *want* to capture the capital letter if it is there but you do *NOT* want the punctuation but you *DO* want the start of text ... UGH!


    Maybe a true RegExp guru can come up with one that works. I can't. Or at least I can't without spending who knows how much time on it.

    Why is it important to be a single RegExp?
    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.

  5. #35
    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
    so close...

    I just need to get rid of that leading full`stop and space before the capital (if there is one)
    My latest version doesn't capture the full-stop and space. It does if you look at the entire capture, but the text you need is in capture group 1.

    Added: To retrieve the captured group 1:
    Code:
    var match = myRegexp.exec(myString);
    alert(match[1]);  // abc
    Last edited by AndrewGSW; 02-01-2013 at 01:10 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

  6. #36
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,984
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Quote Originally Posted by Old Pedant View Post
    "If a capital letter is not found (going backwards from the word to be found), then look for a [.?!]. If a [.?!] is not found look for the beginning of the text."
    that was never a condition. Again:
    - 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.

    I don't want the punctuation at the start. Only at the end, if it's a [.?!]

  7. #37
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Wiki: Sic
    The Latin adverb sic ("thus"; in full: sic erat scriptum, "thus was it written") added immediately after a quoted word or phrase (or a longer piece of text), indicates that the quotation has been transcribed exactly as found in the original source, complete with any erroneous spelling or other nonstandard presentation. The usual purpose is to inform the reader that any errors or apparent errors in the transcribed material do not arise from transcription errors, and the errors have been repeated intentionally, i.e., that they are reproduced exactly as set down by the original writer or printer. It may also be used as a form of ridicule or as a humorous comment, drawing attention to the original writer's spelling mistakes or emphasizing his or her erroneous logic.
    "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

  8. #38
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    I don't want the punctuation at the start. Only at the end,
    I don't recall you mentioning that you want to capture the punctuation at the end so mine doesn't capture it. It should be easy to modify to include it.

    Added: I've edited a recent post of mine to show how you can retrieve capture group 1.
    Last edited by AndrewGSW; 02-01-2013 at 01:11 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

  9. Users who have thanked AndrewGSW for this post:

    xelawho (02-01-2013)

  10. #39
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,984
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I know what sic means. I'm wondering what's wrong with capital letters:
    http://www.britishcouncil.org/learne...al-letters.htm

    [EDIT:] Sweet. Thanks for the example. I think I mentioned I'm a complete nong at regex

  11. #40
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Edited, sorry! Had a missing bracket.
    Code:
    var myvar = "happy";
    var myreg = new RegExp("(?:^|[.;!?])\s*([^.!?]*" + myvar + "[^.!?]*)(?:[.?!]|$)");
    
    var matches = myreg.exec("Hey! Some great and happy sentence! Doh!");
    console.log(matches[1]);
    (untested)
    Last edited by AndrewGSW; 02-01-2013 at 01:22 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

  12. #41
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    My last one! I think I'll ignore spaces at the beginning or end of the sentence and just trim afterwards:

    Code:
    var myvar = "happy";
    var myreg = new RegExp("(?:^|[.;!?])([^.!?]*" + myvar + "[^.!?]*)(?:[.?!]|$)");
    
    var matches = myreg.exec("Hey!   Some great and happy sentence  ! Doh!");
    var sentence = (matches[1]).trim();
    console.log(sentence);    // Some great and happy sentence
    trim() isn't available in some old browsers, so:
    Code:
    if(!String.prototype.trim) {
      String.prototype.trim = function () {
        return this.replace(/^\s+|\s+$/g,'');
      };
    }
    My work here is done!
    Last edited by AndrewGSW; 02-01-2013 at 01:31 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

  13. #42
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,719
    Thanks
    80
    Thanked 4,514 Times in 4,478 Posts
    I think he was using (sic) [sic] in reference to "capital letters" versus "upper case letters."

    But since he used it wrongly (parentheses instead of square brackets), he doesn't score any points. <grin/>
    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. #43
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by Old Pedant View Post
    I think he was using (sic) [sic] in reference to "capital letters" versus "upper case letters."

    But since he used it wrongly (parentheses instead of square brackets), he doesn't score any points. <grin/>
    I shouldn't have to explain this.. but.. my sentence didn't begin within a capital letter, and I was stating that I always begin sentences with a capital letter ..hence.. oh, never mind
    "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

  15. #44
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,984
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I'm not sure that you can quote yourself in the sentence where you are quoting yourself, saying "thus was it written" as you are in the act of writing it.

    Sounds like a little too much recursion to me (my first javascript joke and I am very proud of it).

  16. #45
    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
    I'm not sure that you can quote yourself in the sentence where you are quoting yourself, saying "thus was it written" as you are in the act of writing it.

    Sounds like a little too much recursion to me (my first javascript joke and I am very proud of it).
    Wiki: It may also be used as a form of ridicule or as a humorous comment, drawing attention to the original writer's spelling mistakes or emphasizing his or her erroneous logic
    .
    ..but this is also recursive as I am quoting a quote that I already quoted.
    "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 3 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
  •