Hi Folks,
Is there a way ask a regex if it matches any part of a string? matches: wants an exact match, and matchesPrefix: clearly only matches a prefix. The best I found was to abuse matchesIn: and check the size of the response, but this is not very clean.
Does anybody know if there is a better way?
normalize: url | normal re | ... re := '[^/]+/../' asRegex. [ (re matchesIn: normal) notEmpty ] whileTrue: [ normal := re copy: normal replacingMatchesWith: '' ]. ^ normal
Here I am collapsing ".." navigations within a URL against parent directories, as long as there is such a match. But copy:replacingMatchesWith: considers submatches, whereas matches: does not. Bummer.
---
A second question: I would like to strip a string prefix if it matches. Since this is a literal string, I should not need regexes. But I cannot find a nice existing method to do this. With regexes I must escape all special regex chars:
localize: url "remove prefix if url is for this website" self aliasesAsRegexes do: [ :re | (re matchesPrefix: url) ifTrue: [ ^ re copy: url replacingMatchesWith: '' ] ]. ^ url
This is supposed to delete all known aliases for a given web site from the start of a url to turn them into local urls.
aliasesAsRegexes aliasesAsRegexes ifNil: [ aliasesAsRegexes := aliases collect: [:alias | alias := '/' asRegex copy: alias replacingMatchesWith: '/'. alias := '~' asRegex copy: alias replacingMatchesWith: '~'. alias := ':' asRegex copy: alias replacingMatchesWith: ':'. alias := '.' asRegex copy: alias replacingMatchesWith: '.'. alias asRegex ]]. ^ aliasesAsRegexes
Bleh! I am ashamed to have to write such crappy code.
The Method Finder did not find me a cool method to do this:
'abra' . 'abracadabra' . 'cadabra'
Thanks for any hints.
- on