我需要一个可以在VBScript和.NET中使用的正则表达式,它只返回字符串中的数字.
例如,以下任何"字符串"应仅返回1231231234
这将在电子邮件解析器中用于查找客户可能在电子邮件中提供的电话号码并进行数据库搜索.
我可能错过了类似的正则表达式,但我确实在regexlib.com上搜索.
[编辑] - 在设置musicfreak的答案后添加了由RegexBuddy生成的代码
VBScript代码
Dim myRegExp, ResultString
Set myRegExp = New RegExp
myRegExp.Global = True
myRegExp.Pattern = "[^\d]"
ResultString = myRegExp.Replace(SubjectString, "")
Run Code Online (Sandbox Code Playgroud)
VB.NET
Dim ResultString As String
Try
Dim RegexObj As New Regex("[^\d]")
ResultString = RegexObj.Replace(SubjectString, "")
Catch ex As ArgumentException
'Syntax error in the regular expression
End Try
Run Code Online (Sandbox Code Playgroud)
C#
string …
Run Code Online (Sandbox Code Playgroud) 我需要使用Regex.Replace
从字符串中删除所有数字和符号.
示例输入:123- abcd33
示例输出:abcd
提前致谢.
我需要解析出现在字符串开头的十进制整数.
十进制数字后面可能有尾随垃圾.这需要被忽略(即使它包含其他数字.)
例如
"1" => 1
" 42 " => 42
" 3 -.X.-" => 3
" 2 3 4 5" => 2
Run Code Online (Sandbox Code Playgroud)
.NET框架中是否有内置方法来执行此操作?
int.TryParse()
不适合.它允许尾随空格但不允许其他尾随字符.
实现这个很容易,但如果它存在,我宁愿使用标准方法.
我正在研究.NET项目,我试图只解析字符串中的数值.例如,
string s = "12ACD";
int t = someparefun(s);
print(t) //t should be 12
Run Code Online (Sandbox Code Playgroud)
有几个假设
是否有任何C#预定义函数来解析字符串中的数值?
我有这个字符串:
http://www.edrdg.org/jmdictdb/cgi-bin/edform.py?svc=jmdict&sid=&q=1007040&a=2
Run Code Online (Sandbox Code Playgroud)
我怎样才能挑选出“ q =“和“&amp”之间的数字作为整数?
因此,在这种情况下,我想获取数字:1007040
static void Main(string[] args)
{
string foo = "jason123x40";
char[] foo2 = foo.ToCharArray();
string foo3 = "";
for (int i = 0; i < foo2.Length; i++)
{
int num = 0;
Int32.TryParse(foo2[i].ToString(), out num);
if (num != 0)
{
foo3 += num.ToString();
}
}
Console.WriteLine(foo3);
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
所以假设我有一个名为"john10smith250"的字符串.结果应为"10250".但是我会用我的代码获得"125".
我过滤掉0的原因是因为我不希望任何非数字字符被视为零.
有没有更好的方法将字符串的一部分转换为int?
喜欢:
"Name: Daniel --- Phone Number: 3128623432 --- Age: 12 --- Occupation: Student"
Run Code Online (Sandbox Code Playgroud)
如何获得"年龄"后的内容?我只想要数字.(他的年龄)
我有一个带有单词和数字的字符串.如何找到最后一个数字(位数未知)并将其隔离?直到现在我使用了Substring方法,但是文本中数字的位置以及数字的长度是未知的.
谢谢!
So I'm writing a program for a barcodescanner in C# .NET 3.5
. When I scan the barcode I get an string
with just numbers and I want that string to split at every number and put each number into an int array
, but I can´t figure out how. Does someone of you know how to do that?
我有字符串:
我必须提取"湿度:"之后的所有数字,...我正在考虑使用Regex类,但我不确切知道如何做到这一点
我获取串行数据的代码:
namespace Demo1Arduino
Run Code Online (Sandbox Code Playgroud)
{
public partial class MainWindow : Window
{
private SerialPort port;
DispatcherTimer timer = new DispatcherTimer();
private string buff;
public MainWindow()
{
InitializeComponent();
}
private void btnOpenPort_Click(object sender, RoutedEventArgs e)
{
timer.Tick += timer_Tick;
timer.Interval = new TimeSpan(0, 0, 0, 0, 500);
timer.Start();
try
{
port = new SerialPort(); // Create a new SerialPort object with default settings.
port.PortName="COM4";
port.BaudRate = 115200; // …
Run Code Online (Sandbox Code Playgroud)