Topic on User talk:TweetsFactsAndQueries

Jump to navigation Jump to search

SPARQL tutorial - negate FILTER, REGEX

6
78.51.223.156 (talkcontribs)
SELECT ?item ?itemLabel ?bblid
WHERE {  
    ?item wdt:P2580 ?bblid .
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }  
    FILTER (REGEX(STR(?bblid), "[\\.q]")) 
}

Try it!

  1. NOT before REGEX results in error message, while for FILTER NOT EXISTS it is fine.
  2. NOT FILTER results in error message

I would like to add FILTER and REGEX negation to the tutorial. Any idea?

Apart from that, during testing negation it was found that character class negation as describe at https://www.regular-expressions.info/refcharclass.html does not seem to work:

  1. FILTER REGEX(STR(?bblid), "[0-9]") results in 4037 items listed
  2. FILTER REGEX(STR(?bblid), "[^0-9]") results in 4239 items listed, and lists items where the ID contains [0-9]
TweetsFactsAndQueries (talkcontribs)

The general expression negation operator is the exclamation mark, NOT is just for a few special operators (I think).

SELECT ?item ?itemLabel ?bblid
WHERE {  
    ?item wdt:P2580 ?bblid .
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }  
    FILTER(!REGEX(STR(?bblid), "[\\.q]")) 
}

Try it!

The regex "[^0-9]" matches any string that contains at least one character other than 0 to 9, anywhere. To match IDs that do not contain those characters, you need "^[^0-9]*$" (from start to finish, contains only characters other than 0 to 9).

78.51.223.156 (talkcontribs)

Thanks a lot. So to list values that violate the formatter constraint

[A-Za-z][-.0-9A-Za-z]{1,}

one has to write

FILTER(!REGEX(STR(?bblid), "^[A-Za-z][-.0-9A-Za-z]{1,}$"))

I will think about how to add it to the tutorial and confirm here when done. Thanks a lot!

78.51.248.21 (talkcontribs)
78.51.248.21 (talkcontribs)

A related one, BIND with REGEX on regex-groups, any idea? {{SELECT ?itemLabel ?bbld ?item WHERE {

   ?item wdt:P2580 ?bbld .
   SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }  
   FILTER (REGEX(STR(?bbld), "-[0-9]{4}-[0-9]{4}$"))
   #BIND year of birth and year of death
   #BIND(REGEX(STR(?bbld), "-([0-9]{4})-[0-9]{4}$") AS ?yob).
   #BIND(REGEX(STR(?bbld), "-[0-9]{4}-([0-9]{4})$") AS ?yod).

} }}

78.51.248.21 (talkcontribs)

sorry, still struggling with the user interface...:

SELECT  ?item ?itemLabel ?bbld ?yob ?yod
WHERE {  
   ?item wdt:P2580 ?bbld .
   SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }  
   FILTER (REGEX(STR(?bbld), "-[0-9]{4}-[0-9]{4}$"))
   #BIND year of birth and year of death
   #BIND(REGEX(STR(?bbld), "-([0-9]{4})-[0-9]{4}$") AS ?yob).
   #BIND(REGEX(STR(?bbld), "-[0-9]{4}-([0-9]{4})$") AS ?yod).
}

Try it!

Reply to "SPARQL tutorial - negate FILTER, REGEX"