Hi Oscar,
Oscar Nierstrasz wrote:
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.
Have a look at #search: as I think this will help you.
---
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:
I can't think of anything off the top of my head but the great thing
about smalltalk is you can easily add what you need. For example, you
could use #beginsWith: when adding a method to String.
copyReplacePrefix: prefix with: replacement
self beginsWith: prefix
ifTrue: [^ replacement , (self copyFrom: prefix size to: self size)]
^ self
Then you will be able to do:
'abracadabra' copyReplacePrefix: 'abra' with: ''
Next step would be to write a few unit tests and then attach both as to
an enhancement request at http://bugs.squeak.org.
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
I don't think /, ~ or : have any special meaning so why do you need to
escape them? Even if you did, a better way might be to do:
alias
copyWithRegex: '[/~:.]'
matchesTranslatedUsing: [:ea | '\' , ea]
But if you're just replacing '.' then you can use:
alias copyReplaceAll: '.' with: '\.'
Hope this helps.
Zulq
_______________________________________________
Beginners mailing list
Beginners@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners