在编程中提取正整数的方法取决于你使用的编程语言。以下是一些常见编程语言中提取正整数的方法:
C++
在C++中,可以使用以下方法提取字符串中的正整数:
```cpp
include include include std::vector int tmp_int = 0; std::vector num += ' '; // 防止字符串的最后一位是数字 bool flag = false; // 用于记录数字是否出现 for (size_t i = 0; i < num.size(); ++i) { if (num[i] >= '0' && num[i] <= '9') { flag = true; tmp_int = 10 * tmp_int + (num[i] - '0'); } else if (flag) { flag = false; num_vector.push_back(tmp_int); tmp_int = 0; } } return num_vector; } int main() { std::string input = "123awsd123"; std::vector for (int num : numbers) { std::cout << num << " "; } return 0; } ``` Python 在Python中,可以使用以下方法提取字符串中的正整数: ```python import re def extract_positive_integers(s): return [int(num) for num in re.findall(r'\b[1-9]\d*\b', s)] input_str = "df-23adfd56 2343?23dgjop00535" result = extract_positive_integers(input_str) print(result) ``` Java 在Java中,可以使用以下方法提取字符串中的正整数: ```java import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static List List Pattern pattern = Pattern.compile("\\b[1-9]\\d*\\b"); Matcher matcher = pattern.matcher(input); while (matcher.find()) { numbers.add(Integer.parseInt(matcher.group())); } return numbers; } public static void main(String[] args) { String input = "df-23adfd56 2343?23dgjop00535"; List for (int num : result) { System.out.print(num + " "); } } } ``` C 在C中,可以使用以下方法提取字符串中的正整数: ```csharp using System; using System.Collections.Generic; using System.Text.RegularExpressions; public class Program { public static List { List Regex regex = new Regex(@"\b[1-9]\d*\b"); MatchCollection matches = regex.Matches(input); foreach (Match match in matches) { numbers.Add(int.Parse(match.Value)); } return numbers; } public static void Main() { string input = "df-23adfd56 2343?23dgjop00535"; List foreach (int num in result) { Console.Write(num + " "); } } } ``` 这些示例展示了如何在不同的编程语言中提取字符串中的正整数。你可以根据自己使用的编程语言选择合适的方法。