Reputation: 957
I am trying to grab certain parts of this string:
query = SELECT distinct ?Subject ?Predicate ?Object WHERE {?Subject ?Predicate ?Object . ?Subject limo:hasGender 'man' . ?Subject limo:gender 'Chemically Castrated' . } ORDER BY ASC(?Subject)
I would like to grab all before and including "?Subject limo:hasGender" and then the period right after "man" and the rest of the string. I have tried to do something like this and various other combinations to grab it, but I'm having troubles as I am still new to using regex?
var qmatch = query.match(/(.*?\?Subject limo:hasGender) (?:.*?)(\..*?)/g
Eventually I want my end result so that I can access the data as below:
qmatch[0] = SELECT distinct ?Subject ?Predicate ?Object WHERE {?Subject ?Predicate ?Object . ?Subject limo:hasGender
qmatch[1] = . ?Subject limo:gender 'Chemically Castrated' . } ORDER BY ASC(?Subject)
Thus without the "man" value. I would normally just use a split on "man" but I cannot guarantee that will be the only occurrence of "man" in the query. This is so if I query and choose another hasGender (say woman) than I can just replace that placing(value) "man" in the string to "woman" and yet still keep the rest of the query.
Thank you for any advice anybody may have on this.
Upvotes: 2
Views: 574
Reputation: 1074485
For that very specific situation, the regex is:
^(.*:hasGender)\s*'[^']+'\s*(.*)$
If you use exec
, you access [1]
and [2]
. for the strings you've mentioned.
https://regex101.com/r/lE0rJ4/1
Live Example:
var str = "query = SELECT distinct ?Subject ?Predicate ?Object WHERE {?Subject ?Predicate ?Object . ?Subject limo:hasGender 'man' . ?Subject limo:gender 'Chemically Castrated' . } ORDER BY ASC(?Subject)";
var match = /^(.*:hasGender)\s*'[^']+'\s*(.*)$/.exec(str);
snippet.log("[1]: " + match[1]);
snippet.log("[2]: " + match[2]);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
If you absolutely need [0]
and [1]
instead, then:
match = Array.prototype.slice.call(match, 1);
Live example:
var str = "query = SELECT distinct ?Subject ?Predicate ?Object WHERE {?Subject ?Predicate ?Object . ?Subject limo:hasGender 'man' . ?Subject limo:gender 'Chemically Castrated' . } ORDER BY ASC(?Subject)";
var match = /^(.*:hasGender)\s*'[^']+'\s*(.*)$/.exec(str);
match = Array.prototype.slice.call(match, 1);
snippet.log("[0]: " + match[0]);
snippet.log("[1]: " + match[1]);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
This is so if I query and choose another hasGender (say woman) than I can just replace that placing(value) "man" in the string to "woman" and yet still keep the rest of the query.
That's a slightly different question:
var str = "query = SELECT distinct ?Subject ?Predicate ?Object WHERE {?Subject ?Predicate ?Object . ?Subject limo:hasGender 'man' . ?Subject limo:gender 'Chemically Castrated' . } ORDER BY ASC(?Subject)";
snippet.log("Before: " + str);
str = str.replace(/^(.*:hasGender\s*')[^']+('\s*.*)$/, "$1woman$2");
snippet.log("After: " + str);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Upvotes: 2