亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

正則表達式用兩個點分隔的部分提取主題標簽

正則表達式用兩個點分隔的部分提取主題標簽

嚕嚕噠 2022-12-21 13:11:32
我正在嘗試創建一個正則表達式以便從字符串中提取一些文本。我想從網址或普通短信中提取文本,例如:endpoint/?userId=#someuser.id要么Hi #someuser.name, how are you?我想#someuser.name從消息和#someuser.idurl 中準確提取??赡苡泻芏噙@樣的字符串要從 url 和消息中提取。我的正則表達式目前看起來像這樣:(#[^\.]+?\.)([^\W]\w+\b)它工作正常,除了一對一的情況,我不知道該怎么做 - 例如:這些字符串不應匹配:# .id, #.id. #和之間必須至少有一個字符.。不應匹配這些字符之間的一個或多個空格。我怎樣才能使用我當前的正則表達式來做到這一點?
查看完整描述

4 回答

?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

你可以使用

String regex = "#[^.#]*[^.#\\s][^#.]*\\.\\w+";

請參閱正則表達式演示及其圖表

http://img1.sycdn.imooc.com//63a2959a0001d78f06560126.jpg

細節

  • #- 一個#符號

  • [^.#]*.- 除and之外的零個或多個字符#

  • [^.#\\s]- 任何字符,但.,#和空格

  • [^#.]*.- - 除and之外的零個或多個字符#

  • \.- 一個點

  • \w+- 1+ 個單詞字符(字母、數字或_)。

Java演示

String s = "# #.id\nendpoint/?userId=#someuser.id\nHi #someuser.name, how are you?";

String regex = "#[^.#]*[^.#\\s][^#.]*\\.\\w+";

Pattern pattern = Pattern.compile(regex);

Matcher matcher = pattern.matcher(s);

while (matcher.find()){

    System.out.println(matcher.group(0)); 

輸出:


#someuser.id

#someuser.name


查看完整回答
反對 回復 2022-12-21
?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

重新定義的要求是:

  • 找花樣#A.B

  • A可以是任何東西,除了空格,也不能包含#.

  • B只能是常規的 ASCII 字母或數字

將這些要求轉換為(可能的)正則表達式:

#[^.#]+((?<!#\\s+)\\.)[A-Za-z0-9]+

解釋:

#[^.#]+((?<!#\\s+)\\.)[A-Za-z0-9]+  # The entire capture for the Java-Matcher:

#                                   #  A literal '#' character

 [^.#]+                             #  Followed by 1 or more characters which are NOT '.' nor '#'

       (          \\.)              #  Followed by a '.' character

        (?<!     )                  #  Which is NOT preceded by (negative lookbehind):

            #                       #   A literal '#'

             \\s+                   #   With 1 or more whitespaces

                      [A-Za-z0-9]+  #  Followed by 1 or more alphanumeric characters

                                    #  (PS: \\w+ could be used here if '_' is allowed as well)

測試代碼:


String input = "endpoint/?userId=#someuser.id Hi #someuser.name, how are you? # .id #.id %^*#@*(.H(@EH Ok, # some spaces here .but none here #$p€??@l.$p€??@l that should do it..";

System.out.println("Input: \""+ input + '"');


System.out.println("Outputs: ");

java.util.regex.Matcher matcher = java.util.regex.Pattern.compile("#[^.#]+((?<!#\\s+)\\.)[A-Za-z0-9]+")

                                                         .matcher(input);

while(matcher.find())

  System.out.println('"'+matcher.group()+'"');

在線嘗試。


哪些輸出:


Input: "endpoint/?userId=#someuser.id Hi #someuser.name, how are you? # .id #.id %^*#@*(.H(@EH Ok, # some spaces here .but none here #$p€??@l.$p€??@l that should do it.."

Outputs: 

"#someuser.id"

"#someuser.name"

"#@*(.H"

"# some spaces here .but"


查看完整回答
反對 回復 2022-12-21
?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

您可以嘗試以下正則表達式:

#(\w+)\.(\w+)

演示

筆記:

  • 如果您不想捕獲任何組,請刪除括號。

  • 在你的java正則表達式字符串中你需要轉義每一個\

  • 這給#(\\w+)\\.(\\w+)

  • 如果id僅由數字組成,則可以通過以下方式更改第二\w[0-9]

  • 如果username包含除字母表、數字和下劃線以外的其他字符,則必須更改\w為具有明確定義的所有授權字符的字符類。

代碼示例:

String input = "endpoint/?userId=#someuser.id Hi #someuser.name, how are you? # .id, #.id.";

Matcher m = Pattern.compile("#(\\w+)\\.(\\w+)").matcher(input);

while (m.find()) {

    System.out.println(m.group());

}

輸出:


#someuser.id

#someuser.name


查看完整回答
反對 回復 2022-12-21
?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

#(\w+)[.](\w+)

結果兩組,例如

endpoint/?userId=#someuser.id -> group[0]=someuser and group[1]=id


查看完整回答
反對 回復 2022-12-21
  • 4 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號