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 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Match number after a specific word and colon with a space.

    Hi

    If I have a line in an email body like
    Reference number: 132
    or
    Company reference number: 123

    How can I just match 123?

    I tried using regex but only came up with something that matched number: 123.

    Is regex the best/fastest way anyway?

    The string will come from the body of an email, and normally I could just say
    Code:
     var refNumber = email.body.reference_number;
    But I need to cover any scenario where it's just going to end with "number: "

    Thanks,
    Narelle

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,174
    Thanks
    23
    Thanked 601 Times in 600 Posts
    While regex may be smaller code this string manipulation does work
    IF 'number:' with the colon at the end is in the string. I send the string to uppercase in case 'Number:' is used.

    Code:
    <script type="text/javascript">
    var str = "Company reference number: 123 and a good Number it is.";
    var str = str.toUpperCase();
    var section = str.substring(str.search("NUMBER:")+7).trim();
    var end = section.search(" ");
    var num = section.substr(0,end) ;
    alert(num);  // TO SEE THE ANSWER
    </script>
    You can do checks on the results if you want.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    narelle (01-06-2014)

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,334
    Thanks
    11
    Thanked 587 Times in 568 Posts
    Code:
    str.match(/number:\s+\d+/ig)
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    He said
    I need to cover any scenario where it's just going to end with "number: "
    So why not ensure that is true?

    Code:
    str.match(/number:\s+\d+$/ig)
    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
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    But wait a second. He also said
    I tried using regex but only came up with something that matched number: 123.

    How can I just match 123?
    So I *THINK* what he means is he wants to ensure that the string ENDS WITH number: 123 but he wants to then FIND ONLY the 123 part of that!

    Code:
    var str = "this is number: 8817";
    
    var m = str.match(/number\:\s+(\d+)$/i);
    
    if ( m )
    {
        alert( "found number " + m[1] );
    } else {
        alert( "did not find number");
    }
    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.

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,334
    Thanks
    11
    Thanked 587 Times in 568 Posts
    Quote Originally Posted by Old Pedant View Post
    He said

    So why not ensure that is true?

    Code:
    str.match(/number:\s+\d+$/ig)

    there's not a whole lot of info to go upon in the OP, so a clarification or example would remove doubts about end padding and repetition and allow us to post a working start-to-finish testable solution.

    i took that to mean vs "Company reference number:" or "Reference number:"...

    i would be worried about missing "number: 1234 " (with space) if i had an EO(F/L) ($) operator in my RX.


    the match() will need an additional match() or replace() to extract the numerical data anyway, so that would be a good place to scrub the right-side of the number.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #7
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hello,

    Sorry to create confusion with my request. The line

    External Reference Number: 123
    or
    Reference Number: 123
    or
    any other variation of this, will be in the body of an email that will have more content before, and more content afterwards. Everything after and before will be on a new line (so Reference Number: 123 is on it's own line).

    I do only need to take the number, 123. I just need a match on that '123' so that I can create a new reference in my database record of 123.
    Although maybe in other scenarios in the future, I may have to get a number that contains alpha characters as well. eg.
    External Reference Number: 123ABC

    Hope this helps, and thank you all for your contributions.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    So did you try my answer from post #5?

    **********

    I just need a match on that '123' so that I can create a new reference in my database record of 123.
    But if you are affecting a database record, why are you doing this in JavaScript, at all? Why not do it in PHP or whatever language is interacting with the DB?
    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.

  • Users who have thanked Old Pedant for this post:

    narelle (01-06-2014)

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,334
    Thanks
    11
    Thanked 587 Times in 568 Posts
    if on it's own line, then it becomes:

    Code:
    str.match(/number:\s+\d+/igm)
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #10
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    While regex may be smaller code this string manipulation does work
    IF 'number:' with the colon at the end is in the string. I send the string to uppercase in case 'Number:' is used.

    Code:
    <script type="text/javascript">
    var str = "Company reference number: 123 and a good Number it is.";
    var str = str.toUpperCase();
    var section = str.substring(str.search("NUMBER:")+7).trim();
    var end = section.search(" ");
    var num = section.substr(0,end) ;
    alert(num);  // TO SEE THE ANSWER
    </script>
    You can do checks on the results if you want.
    Hi,

    Thanks for this, it works for me but has some limitations if we have to have an number with only 7 digits/characters...?

    Regards
    Narelle

  • #11
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    But wait a second. He also said
    So I *THINK* what he means is he wants to ensure that the string ENDS WITH number: 123 but he wants to then FIND ONLY the 123 part of that!

    Code:
    var str = "this is number: 8817";
    
    var m = str.match(/number\:\s+(\d+)$/i);
    
    if ( m )
    {
        alert( "found number " + m[1] );
    } else {
        alert( "did not find number");
    }
    Hi,

    Are you expecting this to return 8817? I didn't get that result. When i tested in a regex tested, it returned number: 8817.

    Maybe I'm doing something wrong though.

    Thanks anyway!

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Quote Originally Posted by narelle View Post
    Hi,

    Are you expecting this to return 8817? I didn't get that result. When i tested in a regex tested, it returned number: 8817.

    Maybe I'm doing something wrong though.
    "Maybe" you are. It works for me, so I would assume so.

    http://plopon.com/junk7.html

    Go ahead. Try it.

    You can change the contents of the first text <input> there to see if a value you enter matches the pattern. The value I show there, initially, does.
    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.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    If it's not obvious, you can do a VIEW==>>SOURCE to see the HTML/JavaScript code I used there. Tell me what you managed to do to make it *not* work.
    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.

  • Users who have thanked Old Pedant for this post:

    narelle (01-06-2014)

  • #14
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    If it's not obvious, you can do a VIEW==>>SOURCE to see the HTML/JavaScript code I used there. Tell me what you managed to do to make it *not* work.
    Hi,

    It may be just to do with the way I take the variable? This is how I've written it, as this is the way we usually take content from the email body. I know line 1 and line 3 are correct, as they work in other situations.

    Code:
    var str = email.body_text;
    var refStr = str.match(/number\:\s+(\d+)$/i);
    externalReferenceNumberCreate(current.sys_id, 'Client reference number',refStr, 'Client number');
    So perhaps my syntax in line 2 is incorrect - maybe I need to be doing a regex match and replace type thing, which is far too complicated for me.
    Thanks for your patience.

    I have a workaround with an if/else type statement, as a last resort. So I'm happy to mark this as closed if this is difficult to resolve over a forum when I'm not providing much info about the database etc.

    Thanks
    Narelle

  • #15
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,174
    Thanks
    23
    Thanked 601 Times in 600 Posts
    Quote Originally Posted by narelle View Post
    Hi,

    Thanks for this, it works for me but has some limitations if we have to have an number with only 7 digits/characters...?

    Regards
    Narelle
    The str.search() returns the start of the string "NUMBER:" that is 7 characters long, 8 if you want to get rid of the space The +7 moves the start number to the end of that sub string.

    Is it limited? Only if you want to look for a different sub string, in which case you would need to change "NUMBER:" and the "7" offset.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    narelle (01-07-2014)


  •  
    Page 1 of 2 12 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
    •