8000 Fix Arabic search silently returning no results for queries with regex metacharacters by m86hussein · Pull Request #4022 · quran/quran_android · GitHub
[go: up one dir, main page]

Skip to content

Fix Arabic search silently returning no results for queries with regex metacharacters - #4022

Open
m86hussein wants to merge 1 commit into
quran:mainfrom
m86hussein:fix/arabic-search-regex-metacharacters
Open

m86hussein wants to merge 1 commit into
quran:mainfrom
m86hussein:fix/arabic-search-regex-metacharacters

Conversation

@m86hussein
@m86hussein m86hussein commented Nov 9, 2026 •
Copy link
Copy Markdown
Contributor

Problem

Arabic search silently returns no results whenever the query contains a regex metacharacter.

ArabicCharacterHelper.generateRegex() appends any character that has no lookup-table entry straight into the regex, stripping only [, (, ) (bannedChars). Every other regex metacharacter — * + ? { } ^ $ | . \ ] — is emitted raw. ArabicSearcher.runQuery() then does:

val regexp = ArabicCharacterHelper.generateRegex(originalSearchText)
val pattern = Pattern.compile(regexp)

So a query like * produces the pattern *, and Pattern.compile("*") throws PatternSyntaxException: Dangling meta character '*'. DatabaseHandler.search() wraps the call in a try/catch that logs and returns null, so the entire search silently yields zero results — no error, no matches — for any query containing one of these characters.

Fix

Escape regex metacharacters when they aren't lookup-table entries, so the generated pattern always compiles and the character is matched literally (which is the sensible behavior for a search box).

private const val regexSpecialChars = "\\.*+?^\$|{}]"
...
} else if (it !in bannedChars) {
  if (it in regexSpecialChars) {
    regexBuilder.append('\\')
  }
  regexBuilder.append(it)
}

[, (, ) remain stripped as before (they are only ever used to build the character classes). All regex building happens outside a character class, so only the outside-class metacharacters need escaping.

Tests

Added to ArabicCharacterHelperTest:

  • a query of all metacharacters produces a pattern that compiles (no exception);
  • a lone * compiles and matches a literal * (the previous crash case);
  • metacharacters are escaped alongside Arabic lookup chars, so . matches a literal dot rather than any character.

./gradlew :common:search:testReleaseUnitTest passes (ArabicCharacterHelperTest 23, 0 failures).

AI disclosure: this PR was drafted with GitHub Copilot and opened by @m86hussein, who is responsible for it.

…x metacharacters

ArabicCharacterHelper.generateRegex() appended any character without a lookup-table entry straight into the regex, stripping only [ ( ). Other regex metacharacters (* + ? { } ^ \$ | . \\ ]) were emitted raw, so Pattern.compile() in ArabicSearcher.runQuery() threw a PatternSyntaxException. DatabaseHandler.search() catches it and returns null, so the entire Arabic search silently yields no results whenever the query contains one of these characters (e.g. a stray '*').

Escape these metacharacters when they are not lookup-table entries so the generated pattern always compiles and the character is matched literally.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0