Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

In normal RegEx \l will make the character to the right lowercase. However, it seems like the RegEx in Mathematica does not have this functionality.

For example

StringCases["Abba", RegularExpression["(.).*?\\l\\1"]]

will return the following error message

RegularExpression::msg37: "Mathematica does not support \\L, \\l, \\N, \\P, \\p, \\U, \\u, or \\X in RegularExpression[\!\(\"(.).*?\\\\l\\\\1\"\)]."

Is there some workaround that I can use? Note that (?i) is not sufficient as I need the character to be lowercase only.

Thanks

share|improve this question
    
You can use [a-z] if ASCII is sufficient ... otherwise it might not be possible to use regexes and you may need to resort to using string patterns with _?LowerCaseQ which I think will be slower. –  Szabolcs Oct 28 '13 at 15:26
    
@Szabolcs I believe the tricky part is that \\1 refers to the first byte of the regular expression, which may be also uppercase, but match the lowercase letter at end of the regular expression. –  kirma Oct 28 '13 at 15:37
add comment

2 Answers

up vote 1 down vote accepted

I believe this is what you want.

StringCases["Abba", RegularExpression["(.).*(?=(?i:\\1))[a-z]"]]

(?=(?i:\\1)) is a lookahead which says, "if the next character matches \1 in any case," but then it also has to match the pattern that occupies the place of the next character, which is [a-z] i.e. only lowercase letters.

If it fails to match the outer pair of Aa, it will match the inner pair of bb. I couldn't figure out if this was what you wanted or not, it does seem like it judging from your attempt.

share|improve this answer
add comment

This is not a regular expression (or PCRE), but it's a StringExpression that can be used where RegularExpression qualifies:

StringCases["Abba", 
 x : LetterCharacter ~~ Shortest[___] ~~ 
   y : LetterCharacter /; ToLowerCase[x] == y]

{"Abba"}

LetterCharacters are used instead of _ primarily to keep ToLowerCase meaningful.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.