Home > Software engineering >  Regex - Match full string either when alone or used inside other text
Regex - Match full string either when alone or used inside other text

Time:01-02

https://regex101.com/r/HbuaXz/5

Search strings:

:attack
:attack-bonus

Test cases:

foo :attack
foo :attack bar
foo :attack-bonus bar
foo :attack-bonus
:attack
:attack-bonus

How can I write a Regex so that when I use :attack I get these matches:

foo :attack bar
foo :attack
:attack

And when I use :attack-bonus I get these matches:

foo :attack-bonus bar
foo :attack-bonus
:attack-bonus

I've tried using full string match using:

^:attack$

But it doesn't give a match when :attack is used within a string.

I've also tried with lookbehind/lookahead using both:

(?<=.*):attack(?=.*)
(?=.*):attack(?<=.*)

But it still matches the :attack part in :attack-bonus.

CodePudding user response:

:attack(?=\s|$) should do it, or (?<=\s|^):attack(?=\s|$) if you need to assert the space/beginning at the start (e.g., you want to weed out foo:attack [no space]).

The lookahead at the end contains an alternation looking for whitespace or end of input. The lookbehind at the beginning (if you include it) looks for whitespace or beginning of input.

const strings = [
    "foo :attack",
    "foo :attack bar",
    "foo :attack-bonus bar",
    "foo :attack-bonus",
    ":attack",
    ":attack-bonus",
];

function test(rex) {
    console.log(`Matches for ${rex}:`);
    for (const str of strings) {
        if (rex.test(str)) {
            console.log(str);
        }
    }
}

test(/:attack(?=\s|$)/);
test(/:attack-bonus(?=\s|$)/);
.as-console-wrapper {
    max-height: 100% !important;
}

CodePudding user response:

You may use this regex:

.*:attack(?!\S).*

RegEx Demo 1

Similarly for other string, use:

.*:attack-bonus(?!\S).*

RegEx Demo 2

Explanation:

  • .*: Match 0 or more of any character at start (greedy)
  • :: Match a colon
  • attack: Match text attack:
  • (?!\S): Lookahead assertion to make sure that don't have a non-whitespace character ahead of the current position
  • .*: Match 0 or more of any character at end (greedy)
  • Related