简体   繁体   中英

RegEx to randomly set case of characters in a string

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 (eg, '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.

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());

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

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()

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM