分割字串以提取數字你好 ,
我有一個字元字串,我想提取 4 個數字部分。數字的位置可能會改變
該字串的範例可以是
值=“你好0123範例”;
我已經嘗試過這個,但只有當數字位於正確的位置時才有效。
值=“你好0123範例”;
提取=substr(值,7,4);
由於數字的位置會改變,我該如何提取它?
進階統計建模回复:拆分字符串以提取數字好的謝謝@Aurora_TiffanyD和@jthi !
回复:拆分字符串以提取數字這可能更好,因為您不必搶第一場比賽。謝謝!
回复:拆分字符串以提取數字另一種選擇是使用 Regex 而不是 Regex Match(還有更多選項可以如何完成,但我認為 Regex 和 Regex Match 是最簡單的)
Names Default To Here(1);
string = "Hello 0123 example";
four_num = Regex(string, "\d{4}"); //0123
看https://regex101.com/r/26hBdb/1如果您不熟悉正則表達式,請了解它的工作原理。
回复:拆分字符串以提取數字這就像一個魅力。 太感謝了!
回复:拆分字符串以提取數字你可以嘗試這樣的事情:
Names Default To Here( 1 );
string = "Hello 0123 example";
matches = Regex Match(
string,
"[0-9]{4}",
);
// ASSUMES that you are interested only in the first 4-digit number, if there is more than one
extractedNumber = matches[1];
這將返回“0123”。
請注意,如果您輸入的字符串是“Hello 01234 example”,它仍會返回“0123”。