String str = "USD SBFARE 4067.71 OVDPCT 8P SBMARKUP 0A TPS 59486 CC 0P OTH 0A"
I need "8" (OVDPCT 8P)
from this string.
Number always follow OVDPCT and precede with a P.
This 8 may be 10,12 etc means any number.
How can i by using c#.?
How many different variants are we talking about?
If it's always OVDPCT *P
then the pattern can be:
.*OVDPCT (\d+)P.*
You can use it like this:
Match match = Regex.Match(str,@".*OVDPCT (\d+)P.*");
int num = int.Parse(match.Groups[1].Value);
Note: I'm being very rough here, you'd probably want to check match.Success
and also use int.TryParse
.
So you can use a regular expression to do that
var match = System.Text.RegularExpressions.Regex.Match(str, "OVDPCT (?<Number>\\d+)P", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if(match.Success)
{
var number = match.Groups["Number"].Value;
}
But this line seems like an data base record isn't it?
然后应该可以工作:
.*?OVDPCT (\d+)P.*
\\d+ is the regex for an integer number. So
resultString = Regex.Match(subjectString, @"\d+").Value;
will give you that number as a string. Int32.Parse(resultString) will then give you the number.
Excuse the scrappy code but something like this:
var str = "USD SBFARE 4067.71 OVDPCT 8P SBMARKUP 0A TPS 59486 CC 0P OTH 0A";
var strAsArray = str.Split(" ".ToArray(), StringSplitOptions.RemoveEmptyEntries);
var subject = strAsArray[4].Trim(); // assuming fifth element
var value = new Regex("^[0-9]+").Match(subject).Value;
Console.WriteLine("Found: ", value);
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.