Languages
[Edit]
EN

Java regex word boundary with special characters at the beginning or end of regex (dot, plus, underscore, dash)

6 points
Created by:
Marcin
18610

In this article we would like to show you how to use regex word boundary in java with special characters at the beginning or end of regex.

Practical examples:

import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.assertj.core.api.Assertions.assertThat;

public class RegexWordBoundaryTests {

    @Test
    public void regexWordBoundaryTest1() {
        Pattern pattern = Pattern.compile("\\.NET", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(".NET");
        assertThat(matcher.find()).isTrue();
        assertThat(matcher.group()).isEqualTo(".NET");
    }

    @Test
    public void regexWordBoundaryTest2() {
        Pattern pattern = Pattern.compile("\\.\\bNET\\b", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(".NET");
        assertThat(matcher.find()).isTrue();
        assertThat(matcher.group()).isEqualTo(".NET");
    }

    @Test
    public void regexWordBoundaryTest3() {
        // if word starts with '.' and ends with '.'
        Pattern pattern = Pattern.compile("\\.\\bNET\\b\\.", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher("test .NET. test");
        assertThat(matcher.find()).isTrue();
        assertThat(matcher.group()).isEqualTo(".NET.");
    }

    @Test
    public void regexWordBoundaryTest4() {
        Pattern pattern = Pattern.compile("\\bC\\b#", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher("test C# test");
        assertThat(matcher.find()).isTrue();
        assertThat(matcher.group()).isEqualTo("C#");
    }

    @Test
    public void regexWordBoundaryTest5() {
        Pattern pattern = Pattern.compile("\\bC-sharp\\b", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher("test C-sharp test");
        assertThat(matcher.find()).isTrue();
        assertThat(matcher.group()).isEqualTo("C-sharp");
    }

    @Test
    public void regexWordBoundaryTest6() {
        Pattern pattern = Pattern.compile("\\bC_sharp\\b", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher("test C_sharp test");
        assertThat(matcher.find()).isTrue();
        assertThat(matcher.group()).isEqualTo("C_sharp");
    }

    @Test
    public void regexWordBoundaryTest7() {
        Pattern pattern = Pattern.compile("\\bnode\\.js\\b", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher("test node.js test");
        assertThat(matcher.find()).isTrue();
        assertThat(matcher.group()).isEqualTo("node.js");
    }

    @Test
    public void regexWordBoundaryTest8() {
        // if word starts with '-'
        Pattern pattern = Pattern.compile("-\\bnode\\b", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher("test -node test");
        assertThat(matcher.find()).isTrue();
        assertThat(matcher.group()).isEqualTo("-node");
    }

    @Test
    public void regexWordBoundaryTest9() {
        // if word starts with '-'
        Pattern pattern = Pattern.compile("\\bC\\b\\+\\+", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher("test C++ test");
        assertThat(matcher.find()).isTrue();
        assertThat(matcher.group()).isEqualTo("C++");
    }

    @Test
    public void regexWordBoundaryTest10() {
        Pattern pattern = Pattern.compile("\\bC/C\\b\\+\\+", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher("test C/C++ test");
        assertThat(matcher.find()).isTrue();
        assertThat(matcher.group()).isEqualTo("C/C++");
    }

    @Test
    public void regexWordBoundaryTest11() {
        Pattern pattern = Pattern.compile("\\bC/Cpp\\b", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher("test C/Cpp test");
        assertThat(matcher.find()).isTrue();
        assertThat(matcher.group()).isEqualTo("C/Cpp");
    }
}

Result, all tests passed:

Native Advertising
­čÜÇ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join