从字符串中获取子字符串 [英] Get substring from string
问题描述
你好,
请原谅我,我是C#的新手。
我是想知道是否有人可以指出我正确的方向(逻辑上)
可以获得两点之间的所有角色。
ie - i有一个日志文件(由外部应用程序生成的txt文件)
Hi There,
Please forgive me i'm new to C#.
I was wondering if anyone can point me in the right direction (logicwise)
is possible to get all character between two points.
ie - i have a log file (txt file produced by external app)
3/5/2018 12:00 = Critical = xF"CAPTURE BETWEEN HERE"
我只是尝试捕获并将Capture Between Here存储到新的字符串中,而不是整行。
我知道我可以使用子串(x,y)并定义字符的位置,或者使用IF语句但这会改变,我想知道是否有更好的方法捕获和之间的任何值
对不起,我是.net的新手。
谢谢提前
我的尝试:
我试过了IF语句但是有很多日志变量要写if语句。
我尝试使用子字符串位置,但因为日志更改我永远无法捕获正确的值
我尝试过使用正则表达式模式匹配
I'm only try to capture and store the "Capture Between Here" into a new string, not the entire line.
I know i can use substring(x,y) and define location of the chars, or use a IF statement but this changes, i was wondering is there a better method to capture any value between " and "
Sorry i'm new to .net.
Thanks In Advance
What I have tried:
I've tried IF statement but there are to many log variable to write if statements for.
I've tried using substring location but because the log changes i can never capture the correct value
I've tried using regex pattern matching
MatchCollection matches = Regex.Matches(logstring, regexpattern, RegexOptions.IgnoreCase);
foreach (Match match in matches)
{
if (match.Success)
StringBuilder1.Append(match.Groups[1].Value);
}
im new
im new
推荐答案
如何做到这一点最终取决于日志文件中的所有可能条目。
如果总是在表格中
How to do this finally depends on all the possible entries in the log file.
If that is always in the form
<date> = <severity> = <prefix><capture>
我会找到第二个'='字符并尝试识别(跳过)前缀。这可以使用字符串搜索功能(当前缀总是具有相同的长度)或正则表达式来完成。
但是:
这取决于日志文件格式(当我对一般格式的假设是正确的时,前缀的格式) 。
I would locate the second '=' character and try to identify (skip) the prefix. This can be done using string search functions (when the prefix always has the same length) or a regular expression.
But again:
It depends on the log file format (the format of the prefix when my assumption about the general format is correct).
捕获和之间的任何值的方法
method to capture any value between " and "
如果这意味着要捕获的部分始终用双引号括起来,使用正则表达式匹配非常简单。
正则表达式应该是(未经测试)
If that means that the part to be captured is always enclosed by double quotes it is quite simple using a regular expression match.
The regex should be then (untested)
^.+\"(.+)\"
或者只是使用字符串搜索找到每行的双引号。
[/ EDIT]
or just locate the double quotes using string search for each line.
[/EDIT]
谢谢Jochen!那就做到了!!
我使用第一种方法并找到第二个=字符和之后的字符串,然后删除前缀。这给我留下了我追求的价值!日志文件是一致的所以我应该能够重复使用这个值,无论价值如何。
我可能会考虑使用正则表达式,但因为我是一个菜鸟我认为我可能只是让它沉入其中。感谢您抽出时间来欣赏第二双眼睛/大脑。
Thanks Jochen! That Did it!!
I used the first method and located the second "=" character and the string after that, then removed prefix. this left me with values i was after! The log file is consistent so i should be able to reuse this regardless of the values.
I might look at using a regex but since i'm a noob i think i might just let this sink in. Thank you for taking the time to look at this appreciate the second pair of eyes/brains.
这篇关于从字符串中获取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!