Groovy: Generate random string from given character set

后端 未结 6 484
温柔的废话
温柔的废话 2021-01-31 14:33

Using Groovy, I\'d like to generate a random sequence of characters from a given regular expression.

  • Allowed charaters are: [A-Z0-9]
  • Length o
6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-31 15:26

    For SoupUI users:

    def generator = { String alphabet, int n ->
      new Random().with {
        (1..n).collect { alphabet[ nextInt( alphabet.length() ) ] }.join()
      }
    }
    randomValue = generator( (('A'..'Z')+('0'..'9')+('a'..'z')).join(), 15 )
    testRunner.getTestCase().setPropertyValue("randomNumber", randomValue);
    

提交回复
热议问题