Search code examples
regexsparqlrdfjenalinked-data

Title Case Conversion in SPARQL Query with REGEX


I am one inch away from the solution to my problem. I am attempting title case conversion of strings retrieved via SPARQL. I am using the REPLACE function in combination with LCASE and REGEX:

BIND (replace(lcase(?label), "(\\b[a-z](?!\\s))", ucase("$1") ) as ?title_case)

lcase(?label): all characters in the string becomes lowercase

(\\b[a-z](?!\\s)): matches the first letter of each word in the string

ucase($1): is the backreference to the first letter matched, that act as replacement after turning it into UPPER case.

Expected Result: animal husbandry methods becomes Animal Husbandry Methods

That solution is working almost right, but not quite, for reasons beyond my comprehension; check here an example at work.

When you run the query you won't notice anything different in the ?title_case, but if you edit the ucase("$1") for ucase("aaa") you see it magically replacing correctly the first letter of each word:

Result: animal husbandry methods becomes AAAnimal AAAusbandry AAAethods

It seems to me the UCASE function does not have any affect on the backreference $1

Who can explain to me why so, and what is to do to rectify this behavior?


Solution

  • You can use SUBSTR{} function to solve the issue.

    Eg: BIND (REPLACE(LCASE(?label), "(\\b[a-z](?!\\s))", UCASE(SUBSTR(?label, 1, 1)) ) as ?title_case)