简体   繁体   English

如何使用正则表达式从字符串中提取子字符串?

[英]How can I extract a substring from a string using regular expressions?

Let us say that I have a string "ABCDEF34GHIJKL". 我们假设我有一个字符串“ABCDEF34GHIJKL”。 How would I extract the number (in this case 34) from the string using regular expressions? 如何使用正则表达式从字符串中提取数字(在本例中为34)?

I know little about the regular expressions, and while I would love to learn all there is to know about it, time constraints have forced me to simply find out how this specific example would work. 我对正则表达式知之甚少,虽然我很想学习所有关于它的知识,但时间限制迫使我只是想知道这个具体的例子是如何工作的。

Any information would be greatly appreciated! 任何信息将不胜感激!

Thanks 谢谢

This is a very language specific question but you didn't specify a language. 这是一个特定于语言的问题,但您没有指定语言。 Based on previous questions you've asked though I'm going to assume you meant this to be a C# language question. 根据您之前提出的问题,虽然我会假设您认为这是一个C#语言问题。

For this scenario just write up a regex for a number and apply it to the input. 对于这种情况,只需为数字写一个正则表达式并将其应用于输入。

var match = Regex.Match(input, "\d+");
if ( match.Success ) {
  var number = match.Value;
}

Depends on the language, but you want to match with an expression like ([0-9]+) . 取决于语言,但您想要与([0-9]+)这样的表达式匹配。 That will find (the first group of) digits in the string. 这将在字符串中找到(第一组)数字。 If your regexp engine expects to starts matching at the start of the string, you will need to add .*?([0-9]+) . 如果你的regexp引擎希望在字符串的开头匹配,你需要添加.*?([0-9]+)

I agree with calmh ([0-9]+) is the main thing need to worry about. 我同意冷静([0-9]+)是最需要担心的事情。 However you may want to note that in a lot of languages you'll need to use back references (usually \\\\1 or \\1 ) to get the value. 但是,您可能需要注意,在许多语言中,您需要使用反向引用(通常为\\\\1\\1 )来获取值。 For example 例如

"ABCDEF34GHIJKL".sub(/^.*?([0-9]+).*$/, "\\1")

A better solution in Ruby however would be the following and would also match multiple numbers in the string. 然而,Ruby中的更好的解决方案将是以下,并且还将匹配字符串中的多个数字。

"ABCDEF34GHIJ1001KL".scan(/[0-9]+/) { |m|
    puts m
}

# Outputs:
34
1001

Most languages have some sort of similar methods. 大多数语言都有某种类似的方法。 There are some examples of various languages here http://www.regular-expressions.info/tools.html as well as some good examples of back references being used. 这里有一些各种语言的例子http://www.regular-expressions.info/tools.html以及一些使用后向引用的好例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用javascript正则表达式从字符串中提取子字符串 - Extract substring from string using javascript regular expressions 如何使用正则表达式从字符串中提取 substring? - How do I extract a substring from string using a regular expression? 如何使用正则表达式从输入字符串中提取所有非字母数字字符? - How can I extract all non-alphanumeric characters from an input string using Regular Expressions? 如何使用正则表达式从字符串中提取某个数字? - How can I extract a certain number from a string using Regular expressions? 如何使用正则表达式从字符串中提取RFC1123主机名? - How can I extract RFC1123 hostnames from a string using regular expressions? 如何在.NET中使用正则表达式从字符串中提取子字符串 - How i can extract substring from string using regular expression in .NET 如何用正则表达式提取R中字符串的不匹配部分? - How can I extract the unmatched portion of a string in R with regular expressions? 如何在Java中使用正则表达式从字符串中提取子字符串? - How can i extract substring from the string using regex in Java? 如何使用正则表达式或子字符串从字符串中提取文本? - How can I extract text from a string using regex or substring? 如何使用正则表达式从字符串中提取模式? - How to extract pattern from string using Regular Expressions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM