根据搜索到的单词从句子中捕获前三个和后三个单词

时间:2015-10-28 10:45:44

标签: java regex string

我想从java中的字符串中检索搜索到的单词的前三个和后三个单词。 例如:

 I have a string:
 A quick brown cat jumped over the lazy dog.
 A quick brown monkey move over the lazy dog.
 A quick brown monkey jumped over the lazy dog.

搜索到的字词是:

我希望输出为:

quick brown cat jumped over the lazy 
quick brown monkey jumped over the lazy 

2 个答案:

答案 0 :(得分:0)

使用\S\s的组合来匹配机器人空间和非空格字符。

Pattern.compile("(?:\\S+\\s+){3}" + search_word + "(?:\\s+\\S+){3}");

DEMO

答案 1 :(得分:-1)

此代码适用于您:

public static Body createRunner(World world) {
        BodyDef bodyDef = new BodyDef();

        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(new Vector2(Constants.RUNNER_X, Constants.RUNNER_Y));
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(Constants.RUNNER_WIDTH / 2, Constants.RUNNER_HEIGHT / 2);
        Body body = world.createBody(bodyDef);
        body.setGravityScale(Constants.RUNNER_GRAVITY_SCALE);    
        body.setUserData(new RunnerUserData());  
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;
        fixtureDef.density = 0.0f; 
        fixtureDef.friction = 0.0f;
        fixtureDef.restitution = 0.0f; 
        body.createFixture(fixtureDef);
        body.resetMassData();
        body.setUserData(new RunnerUserData(Constants.RUNNER_WIDTH, Constants.RUNNER_HEIGHT));


        shape.dispose();
        return body;
    }

    public static Body createCoins(World world) {
        EnemyType enemyType = RandomUtils.getRandomEnemyType();
        BodyDef bodyDef = new BodyDef();
        //bodyDef used to set the enemy to make it not be affected when the runner hit
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(new Vector2(enemyType.getX(), enemyType.getY()));
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(enemyType.getWidth() / 2, enemyType.getHeight() / 2);
        Body body = world.createBody(bodyDef);
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;
        fixtureDef.density = -100.0f; 
        fixtureDef.friction = 0.0f;
        fixtureDef.restitution = -50.0f; 
        body.createFixture(fixtureDef);
        body.resetMassData();
        EnemyUserData userData = new EnemyUserData(enemyType.getWidth(), enemyType.getHeight(), enemyType.getRegions());
        body.setUserData(userData);
        shape.dispose();
        return body;
    }

O / P:

public static void main(String[] args) {
        List<String> l1 = new ArrayList<>();
        l1.add("A quick brown cat jumped over the lazy dog.");
        l1.add("A quick brown monkey move over the lazy dog..");
        l1.add("A quick brown monkey jumped over the lazy dog.");

        Pattern pattern = Pattern.compile("\\s((\\w+\\s){3}jumped\\s(\\w+\\s){3})");
        for (String s : l1) {
            Matcher m = pattern.matcher(s);
            while (m.find()) {
                System.out.println(m.group(1));
            }
        }

    }