views:

128

answers:

4

What I'm looking for is a RegEx to mix the character case of a given string for use in functional testing.

Example Input:

The Quick Brown fox jumps over the Lazy Dog.

Sample Expected Output:

tHe QUiCk BrOwn FoX jUMPs OvER tHe lAzY dOg.

It would be nice if the output was different every time the RegEx were applied. For example, given the above input, another time the RegEx were applied, the output could be:

THe qUIcK bROwN fOX JuMpS oVeR THe lAzy DoG.

I know that it would be easy to simply set up a substitution table (e.g., 'x' => 'X', 'B' => 'b'), but that is not what I'm looking for.

I did a Google search for this and came up with nothing, although I know it has been done. I had code (now lost) that did this.

+3  A: 

RegEx is supposed to do something deterministic and not random. In C# you could achieve it using something like this:

var random = new Random();
var result = new string(((IEnumerable<char>)input.ToLowerInvariant())
              .Select(x => random.Next(2) == 0 ? x : Char.ToUpperInvariant(x))
              .ToArray());
Mehrdad Afshari
Seriously. The simple answer is "Don't."
Daniel Straight
@Daniel: I can think of some possible use cases for spammers ;)
Mehrdad Afshari
Thanks for that -- however C# is OS-specific. I'm not running this on a Window machine; mostly it will run on a headless Linux box (installing X + Wine / Mono just to run a functional test is not going to happen).
BryanH
BryanH: Can't agree. It's hardly OS specific. Mono is in default installation of Ubuntu nowadays.
Mehrdad Afshari
+2  A: 

in perl this works

perl -e '$_="The Quick Brown fox jumps over the Lazy Dog";s/([a-zA-Z])/rand()<.5 ? uc($1) : lc($1)/ge;print'

ran it a few times...

  • ThE quIck bRowN FOx juMPs OVer the lAzy DoG
  • ThE QuiCK brOWN FoX jUmpS oVER THE LazY Dog
  • thE QuICK brOwN FOx JumpS OvEr the LAZY dOg
  • the quicK BroWN Fox JUMPS OVEr The laZY DoG
  • THe QUICk brown FOX juMPS OvEr tHe LAzy doG
  • The qUick Brown FoX jUMps oVeR THe LAzy Dog
  • the QuiCK broWn fOx jumPs oveR The LAzy doG
  • tHE QUicK browN fOx jumpS OVer the LaZY dOG
Charlie
Double-plus thank you!
BryanH
A: 

Regular Expressions are great, but they are not always the best solution.

Here is some simple pseudo-script to do what you want:

The only regex here is in the split function - to convert to an array of characters. Since many languages will let you treat a string as an array of characters anyway, that bit might not even be necessary.

MyArray = MyString.split('[\b\B]')

for each ( Character in MyArray )
{
    if ( Random(0,1) > 0.5 )
    {
         Character = Uppercase(Character)
    }
    else
    {
         Character = Lowercase(Character)
    }
}

MyString = MyArray.join()
Peter Boughton
A: 

It doesn't need to be a regexp. In Python:

>>> from random import choice
>>> a = "lorem ipsum dolor sic amet"
>>> ''.join(choice((c.lower, c.upper))() for c in a)
'LoREM ipSum DoloR sic AmEt'
Roberto Bonvallet