正規表示式 匹配第一次出現的字元

2021-10-08 04:19:29 字數 2194 閱讀 3298

本文翻譯自:regex: matching up to the first occurrence of a character

i am looking for a pattern that matches everythinguntilthe first occurrence of a specific character, say a ";"我正在尋找乙個匹配所有內容的模式,直到第一次出現特定字元,比如「;」- asemicolon.-分號

i wrote this:我寫了這個:

/^(.*);/
but it actually matches everything (including the semicolon) until the last occurrence of a semicolon.但它實際上匹配所有內容(包括分號),直到最後一次出現分號。

參考:正規表示式-匹配第一次出現的字元

"/^([^\\/]*)\\/$/"worked for me, to get only top "folders" from an array like:"/^([^\\/]*)\\/$/"對我有用,只能從陣列中獲取頂級「資料夾」:

a/   <- this

a/b/

c/ <- this

c/d/

/d/e/

f/ <- this

try/[^;]*/試試/[^;]*/

googleregex character classesfor details.谷歌regex character classes的詳細資訊。

/^[^;]*/

the [^;] says match anything except a semicolon.[^;]表示匹配除分號之外的任何內容。the square brackets are a set matching operator, it's essentially, match any character in this set of characters, the^at the start makes it an inverse match, so match anything not in this set.方括號是乙個集合匹配運算子,它本質上匹配這組字元中的任何字元,開頭的^使它成為反向匹配,因此匹配不在此集合中的任何內容。

try/[^;]*/試試/[^;]*/

that's a negating character class .這是乙個否定的角色類 。

you need你需要

/[^;]*/
the[^;]is a character class , it matches everything but a semicolon.[^;]是乙個字元類 ,它匹配除分號之外的所有內容。

to cite theperlremanpage:引用perlre頁:

you can specify a character class, by enclosing a list of characters in , which will match any character from the list.您可以通過在中包含乙個字元列表來指定乙個字元類,該列表將匹配列表中的任何字元。if the first character after the "[" is "^", the class matches any character not in the list.如果「[」之後的第乙個字元是「^」,則該類匹配列表中不存在的任何字元。

this should work in most regex dialects.這適用於大多數正規表示式方言。

lua正則匹配 只匹配第乙個 正規表示式匹配

難度 困難 給你乙個字串 s 和乙個字元規律 p,請你來實現乙個支援 和 的正規表示式匹配。匹配任意單個字元 匹配零個或多個前面的那乙個元素 所謂匹配,是要涵蓋 整個 字串 s的,而不是部分字串。說明 s 可能為空,且只包含從 a z 的小寫字母。p 可能為空,且只包含從 a z 的小寫字母,以及字...

正規表示式的字元匹配(一)

正規表示式的單字元匹配 字元功能 匹配任意1個字元 除了 n 匹配 中列舉的字元 d匹配數字,即0 9 d匹配非數字,即不是數字 s匹配空白,即 空格,tab鍵,n r等等 s匹配非空白 w匹配單詞字元,即a z a z 0 9 w匹配非單詞字元 coding utf 8 import re ret...

正規表示式字元匹配

普通字元由所有那些未顯式指定為元字元的列印和非列印字元組成。這包括所有的大寫和小寫字母字元,所有數字,所有標點符號以及一些符號。最簡單的正規表示式是乙個單獨的普通字元,可以匹配所搜尋字串中的該字元本身。例如,單字元模式 a 可以匹配所搜尋字串中任何位置出現的字母 a 這裡有一些單字元正規表示式模式的...