stakowerflol
stakowerflol

Reputation: 1079

Regex in range but without sings

This is a a text: 6:0 FC Bayern Muenchen - Werder Brem
I want to have: FC Bayern Muenchen

My tries:
\s.*- gives FC Bayern Muenchen -
\b\s.*\b- doesn't match anything

Upvotes: 0

Views: 46

Answers (2)

steffen
steffen

Reputation: 16948

Regex: /(?<![a-z ])[a-z ]+(?![a-z ])/i, test it here: https://regexr.com/3vv2b

Explanation:

  1. Negative lookbehind of an unwanted character → (?<![a-z ])
  2. At least one wanted character → [a-z ]+
  3. Negative lookahead of an unwanted character → (?![a-z ])

Example in PHP:

if (preg_match_all('/(?<![a-z ])[a-z ]+(?![a-z ])/i', $test, $matches)) {
        print_r($matches);
}

Output:

Array
(
    [0] => Array
        (
            [0] =>  FC Bayern Muenchen
            [1] =>  Werder Brem
        )
)

Upvotes: 0

Toto
Toto

Reputation: 91430

Have a try with:

\s[^-]+

where [^-] means any character that is not -

Upvotes: 1

Related Questions