简体   繁体   中英

Regular Expressions strip values

From the string "A123.456C-456.789F987321" I need to return

  • A123.456
  • C-456.789
  • F987321

This can either be as 3 individual calls as "A" (or "C" or "F" followed by any decimal number or decimal point or sign ("+" or "-") until the next non-numeric (or decimalpoint or sign) character, or a more generic call of any letter followed by any number or decimal until the next non-numeric (or decimalpoint or sign)

Thanks

Edit:

For clarity what regular expression should I use to in terms of X to return

123.456 Where X = "A"
-456.789 Where X = "C"
987321 Where X = "F"

From the string "A123.456C-456.789F987321"

Try this one :

(\w\-?[\d\.]+)

Explanation :

\w match any word character [a-zA-Z0-9_]
\-? matches the character - literally
       Quantifier: Between zero and one time, as many times as possible, giving back as needed [greedy]
[\d\.]+ match a single character present in the list below
       Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\d match a digit [0-9]
\. matches the character . literally

g modifier: global. All matches (don't return on first match)

Demo :

http://regex101.com/r/iG6lD1

You are doing overlapping regex matching. Your regex will be :

(?=([A-Z][^A-Z]+))

Here it is picking a capital letter and after than non capital letters as a group.

You can easily modify this example as per your need(alphanumeric with other characters!)

void Main(string[] args)
{
        var regVal  = "A123.456C-456.789F987321";

        string pattern =@"([A-Z][-]*[0-9|.]+)";
        foreach (Match match in Regex.Matches(regVal, pattern, RegexOptions.IgnoreCase))
        Console.WriteLine(match.Groups[1].Value);

}

output is :

A123.456

C-456.789

F987321

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM