0

I' working with nodes and I need to apply a UNIX-type of grep command to a body response I'm getting after an HTTP GET. Here is the incoming text (body variable):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <om2m:discovery xmlns:om2m="http://uri.etsi.org/m2m" xmlns:xmime="http://www.w3.org/2005/05/xmlmime">
    <om2m:matchSize>65</om2m:matchSize>
    <om2m:discoveryURI>
        <reference>gscl</reference>
        <reference>gscl/scls</reference>
        <reference>gscl/scls/subscriptions</reference>
        <reference>gscl/scls/mgmtObjs</reference>
        <reference>gscl/scls/mgmtObjs/subscriptions</reference>
        <reference>gscl/applications</reference>
        <reference>gscl/applications/subscriptions</reference>
        <reference>gscl/applications/mgmtObjs</reference>
        <reference>gscl/applications/mgmtObjs/subscriptions</reference>
        <reference>gscl/accessRights</reference>
        <reference>gscl/accessRights/subscriptions</reference>
        <reference>gscl/groups</reference>
        <reference>gscl/groups/subscriptions</reference>
        <reference>gscl/containers</reference>
        <reference>gscl/containers/subscriptions</reference>
        <reference>gscl/subscriptions</reference>
        <reference>gscl/accessRights/AR_ADMIN</reference>
        <reference>gscl/accessRights/AR_ADMIN/subscriptions</reference>
        <reference>gscl/scls/nscl</reference>
        <reference>gscl/scls/nscl/applications</reference>
        <reference>gscl/scls/nscl/applications/subscriptions</reference>
        <reference>gscl/scls/nscl/applications/mgmtObjs</reference>
        <reference>gscl/scls/nscl/applications/mgmtObjs/subscriptions</reference>
        <reference>gscl/scls/nscl/accessRights</reference>
        <reference>gscl/scls/nscl/accessRights/subscriptions</reference>
        <reference>gscl/scls/nscl/groups</reference>
        <reference>gscl/scls/nscl/groups/subscriptions</reference>
        <reference>gscl/scls/nscl/containers</reference>
        <reference>gscl/scls/nscl/containers/subscriptions</reference>
        <reference>gscl/scls/nscl/subscriptions</reference>
        <reference>gscl/scls/nscl/attachedDevices</reference>
        <reference>gscl/scls/nscl/attachedDevices/subscriptions</reference>
        <reference>gscl/scls/nscl/notificationChannels</reference>
        <reference>gscl/scls/nscl/mgmtObjs</reference>
        <reference>gscl/scls/nscl/mgmtObjs/subscriptions</reference>
        <reference>gscl/scls/nscl/m2mPocs</reference>
        <reference>gscl/applications/98191axla</reference>
        <reference>gscl/applications/98191axla/subscriptions</reference>
        <reference>gscl/applications/98191axla/containers</reference>
        <reference>gscl/applications/98191axla/containers/subscriptions</reference>
        <reference>gscl/applications/98191axla/accessRights</reference>
        <reference>gscl/applications/98191axla/accessRights/subscriptions</reference>
        <reference>gscl/applications/98191axla/groups</reference>
        <reference>gscl/applications/98191axla/groups/subscriptions</reference>
        <reference>gscl/applications/98191axla/notificationChannels</reference>
        <reference>gscl/applications/98191axla/containers/SENSOR </reference>
        <reference>gscl/applications/98191axla/containers/SENSOR/contentInstances</reference>
        <reference>gscl/applications/98191axla/containers/SENSOR/contentInstances/subscriptions</reference>
        <reference>gscl/applications/98191axla/containers/SENSOR/subscriptions</reference>
        <reference>gscl/applications/98191axla/containers/SENSOR/contentInstances/CI_23326802</reference>
        <reference>gscl/applications/98191axla/containers/DATA</reference>
        <reference>gscl/applications/98191axla/containers/DATA/contentInstances</reference>
        <reference>gscl/applications/98191axla/containers/DATA/contentInstances/subscriptions</reference>
        <reference>gscl/applications/98191axla/containers/DATA/subscriptions</reference>
        <reference>gscl/applications/98191axla/containers/DATA/contentInstances/CI_566628946</reference>
        <reference>gscl/applications/98191axla/containers/CONT_81747241</reference>
        <reference>gscl/applications/98191axla/containers/CONT_81747241/contentInstances</reference>
        <reference>gscl/applications/98191axla/containers/CONT_81747241/contentInstances/subscriptions</reference>
        <reference>gscl/applications/98191axla/containers/CONT_81747241/subscriptions</reference>
        <reference>gscl/applications/98191axla/containers/SENSOR/contentInstances/CI_285448423</reference>
        <reference>gscl/applications/98191axla/containers/CONT_360523761</reference>
        <reference>gscl/applications/98191axla/containers/CONT_360523761/contentInstances</reference>
        <reference>gscl/applications/98191axla/containers/CONT_360523761/contentInstances/subscriptions</reference>
        <reference>gscl/applications/98191axla/containers/CONT_360523761/subscriptions</reference>
        <reference>gscl/applications/98191axla/containers/DATA/contentInstances/CI_395775165</reference>
    </om2m:discoveryURI> </om2m:discovery>

From that text, I need to extract the ID between applications and containers, which, in this case, equals 98191axla. I've attempted to do so using the match method:

var searchFor="\s<reference>gscl\/applications\.\d\/DATA\/contentInstances\/subscriptions<\/reference>";
      console.log("Looking for:"); console.log(searchFor);
      var found = body.match(searchFor);

Which always returns -1. I've never worked with regex before, so I had to try different things...none of them have worked. Any help is much appreciated!

Thanks! (btw, I know it is a xml representation, but it is not a complex one with very different children, so I thought manipulating the text would be easier...)

Dankex
  • 3
  • 2
  • [You cant/shouldnt try to parse xml/html with regex](http://stackoverflow.com/a/1732454/560593), instead use something that is made to actually parse the language. For instance there is a [xml2js](https://www.npmjs.org/package/xml2js) module you can import and use – Patrick Evans Sep 24 '14 at 23:53
  • I've tried a few libs (xml2js, xmldoc and libxmljs), but I just thought that for this particular application and xml representation, regex would be easier... – Dankex Sep 25 '14 at 00:55

3 Answers3

1

You can use this code:

var re = /<reference>gscl\/applications\/(\w+)\/containers\/DATA\/congtentInstances\/subscriptions<\/reference>/;
var m = str.match(re);
if (m) console.log(m[1]);

Note that a regexp object is declared between slashes (without enclosing the pattern between quotes).

The pattern use the \w character class that contains ascii digits and letters (and the underscore). This class is followed by the quantifier + that means one or more. Finally, to isolate the substring you need, a capture group is used.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
0

I'm not a Regex expert, but the following seems to work on this data set.

//Your data simulated. 
var data = document.getElementById('data').innerHTML; 

var myRegexp = /\/applications\/([0-9a-zA-Z]*)\/containers/g;

//Group iteration from: http://stackoverflow.com/a/432503/402706
var match = myRegexp.exec(data);
while (match != null) {
    //write out each id that was found
    console.log(match[1]);
    // matched text: match[0]
    // match start: match.index
    // capturing group n: match[n]
    match = myRegexp.exec(data);
}
<script type='text' id='data'>
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <om2m:discovery xmlns:om2m="http://uri.etsi.org/m2m" xmlns:xmime="http://www.w3.org/2005/05/xmlmime">
        <om2m:matchSize>65</om2m:matchSize>
        <om2m:discoveryURI>
            <reference>gscl</reference>
            <reference>gscl/scls</reference>
            <reference>gscl/scls/subscriptions</reference>
            <reference>gscl/scls/mgmtObjs</reference>
            <reference>gscl/scls/mgmtObjs/subscriptions</reference>
            <reference>gscl/applications</reference>
            <reference>gscl/applications/subscriptions</reference>
            <reference>gscl/applications/mgmtObjs</reference>
            <reference>gscl/applications/mgmtObjs/subscriptions</reference>
            <reference>gscl/accessRights</reference>
            <reference>gscl/accessRights/subscriptions</reference>
            <reference>gscl/groups</reference>
            <reference>gscl/groups/subscriptions</reference>
            <reference>gscl/containers</reference>
            <reference>gscl/containers/subscriptions</reference>
            <reference>gscl/subscriptions</reference>
            <reference>gscl/accessRights/AR_ADMIN</reference>
            <reference>gscl/accessRights/AR_ADMIN/subscriptions</reference>
            <reference>gscl/scls/nscl</reference>
            <reference>gscl/scls/nscl/applications</reference>
            <reference>gscl/scls/nscl/applications/subscriptions</reference>
            <reference>gscl/scls/nscl/applications/mgmtObjs</reference>
            <reference>gscl/scls/nscl/applications/mgmtObjs/subscriptions</reference>
            <reference>gscl/scls/nscl/accessRights</reference>
            <reference>gscl/scls/nscl/accessRights/subscriptions</reference>
            <reference>gscl/scls/nscl/groups</reference>
            <reference>gscl/scls/nscl/groups/subscriptions</reference>
            <reference>gscl/scls/nscl/containers</reference>
            <reference>gscl/scls/nscl/containers/subscriptions</reference>
            <reference>gscl/scls/nscl/subscriptions</reference>
            <reference>gscl/scls/nscl/attachedDevices</reference>
            <reference>gscl/scls/nscl/attachedDevices/subscriptions</reference>
            <reference>gscl/scls/nscl/notificationChannels</reference>
            <reference>gscl/scls/nscl/mgmtObjs</reference>
            <reference>gscl/scls/nscl/mgmtObjs/subscriptions</reference>
            <reference>gscl/scls/nscl/m2mPocs</reference>
            <reference>gscl/applications/98191axla</reference>
            <reference>gscl/applications/98191axla/subscriptions</reference>
            <reference>gscl/applications/98191axla/containers</reference>
            <reference>gscl/applications/98191axla/containers/subscriptions</reference>
            <reference>gscl/applications/98191axla/accessRights</reference>
            <reference>gscl/applications/98191axla/accessRights/subscriptions</reference>
            <reference>gscl/applications/98191axla/groups</reference>
            <reference>gscl/applications/98191axla/groups/subscriptions</reference>
            <reference>gscl/applications/98191axla/notificationChannels</reference>
            <reference>gscl/applications/98191axla/containers/SENSOR </reference>
            <reference>gscl/applications/98191axla/containers/SENSOR/contentInstances</reference>
            <reference>gscl/applications/98191axla/containers/SENSOR/contentInstances/subscriptions</reference>
            <reference>gscl/applications/98191axla/containers/SENSOR/subscriptions</reference>
            <reference>gscl/applications/98191axla/containers/SENSOR/contentInstances/CI_23326802</reference>
            <reference>gscl/applications/98191axla/containers/DATA</reference>
            <reference>gscl/applications/98191axla/containers/DATA/contentInstances</reference>
            <reference>gscl/applications/98191axla/containers/DATA/contentInstances/subscriptions</reference>
            <reference>gscl/applications/98191axla/containers/DATA/subscriptions</reference>
            <reference>gscl/applications/98191axla/containers/DATA/contentInstances/CI_566628946</reference>
            <reference>gscl/applications/98191axla/containers/CONT_81747241</reference>
            <reference>gscl/applications/98191axla/containers/CONT_81747241/contentInstances</reference>
            <reference>gscl/applications/98191axla/containers/CONT_81747241/contentInstances/subscriptions</reference>
            <reference>gscl/applications/98191axla/containers/CONT_81747241/subscriptions</reference>
            <reference>gscl/applications/98191axla/containers/SENSOR/contentInstances/CI_285448423</reference>
            <reference>gscl/applications/98191axla/containers/CONT_360523761</reference>
            <reference>gscl/applications/98191axla/containers/CONT_360523761/contentInstances</reference>
            <reference>gscl/applications/98191axla/containers/CONT_360523761/contentInstances/subscriptions</reference>
            <reference>gscl/applications/98191axla/containers/CONT_360523761/subscriptions</reference>
            <reference>gscl/applications/98191axla/containers/DATA/contentInstances/CI_395775165</reference>
        </om2m:discoveryURI> </om2m:discovery>
</script>
Brandon Boone
  • 16,281
  • 4
  • 73
  • 100
  • Interesting solution. Will definitely use this, as it is simple to tweak to work better for different unique ids. – Dankex Sep 25 '14 at 01:02
0

You can do it like this:

var data = '<reference>gscl/applications/98191axla/containers/CONT_81747241</reference>';

console.log(data.match(/\/applications\/([^]+)(?=\/containers)/i)[1]);

Basically, the parentheses are going to capture the text between /applications/ and /containers

LiquidPony
  • 2,188
  • 1
  • 17
  • 19