开发者

Detecting specific string whether in start, middle or end of a string with Regular Expressions

I've been reading some Q&A about regular expressions but I hav开发者_JAVA百科en't found that answer my question. I'll be using ra as the searched string.

My problem is that I want to find the string 'ra' in any string, 'ra' will be replaced with 'RA', but the thing is that I just want to replace 'ra' as long is not part of any other word, for example: order_ra replaced to order RA but camera cannot be replaced with cameRA.

I tried all ready with [\s|_]ra(?:[\s|_]) and does not work, because is looking for anything like order_ra or order ra with an space at the end. I would like to match order ra or order_ra either it has a white space after it or not. Can anyone help me on this? I'm not too literate with regular expressions.

The reason I'm needing this is because I want to capitalize 'ra' dynamically in a string sent by a user interaction but not if belong to a word like came*ra* or *ra*dical. I don't know if I explain myself clearly, excuse me if I'm not.


Usually, you would use word boundaries: \bra\b only matches ra on its own, not inside a word. Unfortunately, the underscore is treated as an alphanumeric character, so index_ra would not be matched.

Therefore you need to implement this yourself. Assuming that your regex dialext supports Unicode and lookaround assertions, use

(?<!\p{L})foo(?!\p{L})

This matches foo, but not foobar or bazfoo:

(?<!\p{L}) # Assert that there is no letter before the current position
foo        # Match foo
(?!\p{L})  # Assert that there is no letter after the current position

If you can't use Unicode character classes, try this:

(?<![^\W\d_])foo(?![^\W\d_])

This is a bit contorted logic (triple negative for teh win!): [^\W\d_] matches a letter (= a character that is not a non-alphanumeric character and not a digit or underscore), so the negative lookaround assertions make sure that there are no letters around the search string ("not a not a (non-alphanumeric or digit or underscore)"). Twisted but necessary since we also want start and end of the string match here.


If I understand what you are looking for, the following will perform the match. The non-capturing group is specified in the parens with (?:...). It is similar to the OP but also includes beginning and end-of-line anchors.

(?:^|\s|_)ra(?:$|\s|_)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜