2

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.

1
  • 1
    If you "know" so much about the string, why don't you just rebuild it with the proper value (man/woman)? Commented Aug 24, 2015 at 22:12

1 Answer 1

2

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>

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you T.J. for your wonderful answer. Took me a few minutes to parse and understand your regex, but I fully understand it now. I do have one question though. Do I need "exec" or is it just more of a safeguard for if there is no matches and thus will produce a null result?
@dlchang: I'm not quite sure I understand the question (late here, headed for bed), but if you want to know whether you've actually made a replacement, you could keep a copy of the original string and compare, or do an if (rex.test(str)) to see if the rex will match anything.
Don't worry T.J., I was just confused about using "exec" or "match", but they're basically accomplishing the same thing for what I want to do, in slightly different ways (if I understand the documentation correctly).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.