小编afo*_*tcu的帖子

正则表达式模式匹配字符串中的正数和负数值

我有模式/匹配行来转换输入字符串,如下所示:

1 3 Hi [2 1 4]
Run Code Online (Sandbox Code Playgroud)

进入这样的数组:

[0] => "1"
[1] => "3"
[2] => "Hi"
[3] => "2 1 4"
Run Code Online (Sandbox Code Playgroud)

那是代码:

String input = sc.nextLine();

Pattern p = Pattern.compile("(?<=\\[)[^\\]]+|\\w+");
Matcher m = p.matcher(input);
List<String> cIn = new ArrayList<String>();
while(m.find()) cIn.add(m.group());
Run Code Online (Sandbox Code Playgroud)

现在我意识到有时候我会得到一些负值,输入就像4 2 -1 2.由于输入是一个String,我不能真正使用任何正则表达式来获得该负值.

我在下面的代码中使用

Integer.parseInt(cIn.get(0)); 
Run Code Online (Sandbox Code Playgroud)

将该字符串值转换为Integer,这实际上就是我需要的.

你能想出一种方法可以让我把-char和char数字保持在一起吗?然后我会检查是否有-字符转换数字并乘以它-1.(如果有更好的方式,我会很高兴听到).

像往常一样,请原谅我的英语.

java regex pattern-matching negative-number

5
推荐指数
1
解决办法
4920
查看次数

单元测试学说:不能模拟存储库方法

我正在尝试模拟 Doctrine 存储库和 entityManager,但 PHPUnit 一直告诉我:

1) CommonUserTest::testGetUserById 尝试配置方法“findBy”,因为它不存在,没有被指定,是最终的,或者是静态的,所以无法配置

这是片段:

<?php
use \Domain\User as User;
use PHPUnit\Framework\TestCase;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\EntityManager;

class CommonUserTest extends PHPUnit_Framework_TestCase
{    
    public function testGetUserById()
    {
        // mock the repository so it returns the mock of the user (just a random string)
        $repositoryMock = $this
            ->getMockBuilder(EntityRepository::class)
            ->disableOriginalConstructor()
            ->getMock();

        $repositoryMock->expects($this->any())
            ->method('findBy')
            ->willReturn('asdasd');

        // mock the EntityManager to return the mock of the repository
        $entityManager = $this
            ->getMockBuilder(EntityManager::class)
            ->disableOriginalConstructor()
            ->getMock();

        $entityManager->expects($this->any())
            ->method('getRepository')
            ->willReturn($repositoryMock);

        // test the user method …
Run Code Online (Sandbox Code Playgroud)

php testing phpunit doctrine doctrine-orm

4
推荐指数
1
解决办法
4230
查看次数