1

のような文字列を持つ

booboo-en
booboo-duck-en
booboo-the-duck-en
booboo-the-duck-so-nice-en

-en取得する最後の文字列 ( ) を除くすべての文字列 (区切られたグループの数に関係なく) を抽出しようとしました。

booboo
booboo-duck
booboo-the-duck

等々...

使用する

^((?:[^-]+-){2}[^-]+).*$

2 つのグループを抽出できますが、任意の数のグループに対してそれを変更する方法がわかりません...

4

2 に答える 2

1

なぜこれに正規表現を使用しているのですか?

少なくともどのスクリプト言語にも分割プリミティブがあります

彼らは通常、配列を返します

分割と配列の最後の要素の削除の組み合わせは簡単です

于 2012-11-18T17:23:20.647 に答える
0

これを試して:

^.*(?=-[^-]*$)

これにより、最後のダッシュの直前まで文字列が一致します。

^       # Start of string
.*      # Match any number of characters
(?=     # until it's possible to match:
 -      #  a dash
 [^-]*  #  followed only by characters other than dashes
 $      #  until the end of the string.
)       # (End of lookahead assertion)
于 2012-11-18T17:15:16.670 に答える