#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Simple regex to pull value from JSON payload.


    First let me apologize for being a total newbiew to regex.

    I am sure this is really simple, and any help is greatly appreciated!

    The value I am looking to extract exists on its own line, and is always in the same format, so should be pretty straight forward.

    This is how the line of data looks in the JSON file:
    "Number": "15806"

    I need the number value only.

    Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Code:
    .*?Number...(\d+)
    Matcher group 1 contains the value you need.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0
    Originally Posted by noobie1000
    Code:
    .*?Number...(\d+)
    Matcher group 1 contains the value you need.
    Thank you for the reply, but I tried this in several different testers and it did not produce any matches.

    I modified what you had and was able to match the Entire string using:
    .*?Number....\d\d\d\d\d.

    But that is NOT what I need... I need the match to come back ONLY as 15806 in this example, obviously that number can be anything between 00000-99999.

    Does some kind of search/replace need to happen to return only the 15806 value in this instance?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    You need the parens to capture the number.

    Code:
    Number":\s+"(\d+)
    Check on how to access the capture in your programming language.
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Will the regex madness ever stop?

    JSON is a common data format. It's well-defined, well-understood and widely used. This means that every mainstream programming language has a JSON parser. It doesn't matter whether you use PHP, Perl, C or this Microsoft stuff: Your language has a parser for JSON. Either built-in or provided by a third-party library.

    Use it, for heaven's sake! Parse the JSON file, fetch your data and be happy. It's quick, it's simple, and it's robust.

    There's absolutely no reason whatsoever to scrape the data with some stupid regex. Why would you do that? Do you love the cryptic syntax so much? Do you feel bad about using premade solutions? Don't. Programming is all about choosing the right tool. Only fools create everything from ground up.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0
    Originally Posted by Jacques1
    Will the regex madness ever stop?

    JSON is a common data format. It's well-defined, well-understood and widely used. This means that every mainstream programming language has a JSON parser. It doesn't matter whether you use PHP, Perl, C or this Microsoft stuff: Your language has a parser for JSON. Either built-in or provided by a third-party library.

    Use it, for heaven's sake! Parse the JSON file, fetch your data and be happy. It's quick, it's simple, and it's robust.

    There's absolutely no reason whatsoever to scrape the data with some stupid regex. Why would you do that? Do you love the cryptic syntax so much? Do you feel bad about using premade solutions? Don't. Programming is all about choosing the right tool. Only fools create everything from ground up.
    The madness will most certainly stop as soon as the pompous megalomaniacs with nothing to contribute and excessive time on their hands stop trolling.

    If you have ever worked in IT and had a POS 3rd party software thrown at you by some disillusioned, disconnected blowhard bureaucrat, such as yourself, you might actually understand why were forced into using less than desirable means to solve a problem.

    In the meantime while we wait for the regex madness to stop, and trolls to stop trolling may I kindly suggest you GFY...

    Comments on this post

    • Jacques1 disagrees : How 'bout learning and improving instead of insulting people?
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by guht
    I kindly suggest you GFY...
    ... says the guy who can't put together a single regex.

    Yeah, good for you, amateur.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo