C# 子串提取问题

发布于 2024-08-03 18:57:25 字数 2758 浏览 3 评论 0原文

我有这样的文本....

======== 1079.tif
Image Description               : Vexcel-UCD-Level-3
------------------
CAM_ID:                  UCD-SU-1-0018 [5]
RECORD_GUID:             64763E99-3573-43AD-995B-8A07E3FE2BE3
IMG_NO:                  1079
CAPTURE_TIME:            2004/03/15 02:07:17.641
IMG_TYPE:                High resolution multi channel RGBI
ROTATION:                0 [degrees]
--- Inner Orientation ---------------
PRINCIPLE_DISTANCE:      101.400 [mm]
PRINCIPLE_POINT_X:         0.000 [mm]
PRINCIPLE_POINT_Y:         0.180 [mm]
PIXEL_SIZE_WIDTH:          9.000 [microns]
PIXEL_SIZE_HEIGTH:         9.000 [microns]
SENSOR_AREA_WIDTH:       103.500 [mm]
SENSOR_AREA_HEIGHT:       67.500 [mm]
-------------------------------------
--- Custom Meta Data ----------------
FMS No:                   1079
Date:                     070912

Time:                     122005
Project:                  QOM
Area:                     QANAVAT
Line No:                  11
Segment No:               1
Waypoint No:              17
WGS84 Latitude:           N34.559857
WGS84 Longitude:          E050.760726
WGS84 Altitude [m]:       1719.1
Pos Solution:             GPS
Track [degree]:           271
Midpulse correction [s]:  0.00086
-------------------------------------

======== 1080vv.TIF
Image Description               : Vexcel-UCD-Level-3
------------------
CAM_ID:                  UCD-SU-1-0018 [5]
RECORD_GUID:             64763E99-3573-43AD-995B-8A07E3FE2BE3
IMG_NO:                  1080
CAPTURE_TIME:            2004/03/15 02:07:19.974
IMG_TYPE:                High resolution multi channel RGBI
ROTATION:                0 [degrees]
--- Inner Orientation ---------------
PRINCIPLE_DISTANCE:      101.400 [mm]
PRINCIPLE_POINT_X:         0.000 [mm]
PRINCIPLE_POINT_Y:         0.180 [mm]
PIXEL_SIZE_WIDTH:          9.000 [microns]
PIXEL_SIZE_HEIGTH:         9.000 [microns]
SENSOR_AREA_WIDTH:       103.500 [mm]
SENSOR_AREA_HEIGHT:       67.500 [mm]
-------------------------------------
--- Custom Meta Data ----------------
FMS No:                   1080
Date:                     070912
Time:                     122008
Project:                  QOM
Area:                     QANAVAT
Line No:                  11
Segment No:               1
Waypoint No:              16
WGS84 Latitude:           N34.559901
WGS84 Longitude:          E050.758750
WGS84 Altitude [m]:       1717.9
Pos Solution:             GPS
Track [degree]:           272
Midpulse correction [s]:  0.00086
-------------------------------------

如您所见,它有一个循环,该循环重复

我需要编写一个 C# 程序以从我的 txt 文件中提取所有这些类型的子字符串的

"1080"
"Longitude:   E050.758750."
"Latitude :     N34.559901."
[m]: 1717.9"

信息,有谁可以帮助我吗? ????

谢谢

i have a text like this ....

======== 1079.tif
Image Description               : Vexcel-UCD-Level-3
------------------
CAM_ID:                  UCD-SU-1-0018 [5]
RECORD_GUID:             64763E99-3573-43AD-995B-8A07E3FE2BE3
IMG_NO:                  1079
CAPTURE_TIME:            2004/03/15 02:07:17.641
IMG_TYPE:                High resolution multi channel RGBI
ROTATION:                0 [degrees]
--- Inner Orientation ---------------
PRINCIPLE_DISTANCE:      101.400 [mm]
PRINCIPLE_POINT_X:         0.000 [mm]
PRINCIPLE_POINT_Y:         0.180 [mm]
PIXEL_SIZE_WIDTH:          9.000 [microns]
PIXEL_SIZE_HEIGTH:         9.000 [microns]
SENSOR_AREA_WIDTH:       103.500 [mm]
SENSOR_AREA_HEIGHT:       67.500 [mm]
-------------------------------------
--- Custom Meta Data ----------------
FMS No:                   1079
Date:                     070912

Time:                     122005
Project:                  QOM
Area:                     QANAVAT
Line No:                  11
Segment No:               1
Waypoint No:              17
WGS84 Latitude:           N34.559857
WGS84 Longitude:          E050.760726
WGS84 Altitude [m]:       1719.1
Pos Solution:             GPS
Track [degree]:           271
Midpulse correction [s]:  0.00086
-------------------------------------

======== 1080vv.TIF
Image Description               : Vexcel-UCD-Level-3
------------------
CAM_ID:                  UCD-SU-1-0018 [5]
RECORD_GUID:             64763E99-3573-43AD-995B-8A07E3FE2BE3
IMG_NO:                  1080
CAPTURE_TIME:            2004/03/15 02:07:19.974
IMG_TYPE:                High resolution multi channel RGBI
ROTATION:                0 [degrees]
--- Inner Orientation ---------------
PRINCIPLE_DISTANCE:      101.400 [mm]
PRINCIPLE_POINT_X:         0.000 [mm]
PRINCIPLE_POINT_Y:         0.180 [mm]
PIXEL_SIZE_WIDTH:          9.000 [microns]
PIXEL_SIZE_HEIGTH:         9.000 [microns]
SENSOR_AREA_WIDTH:       103.500 [mm]
SENSOR_AREA_HEIGHT:       67.500 [mm]
-------------------------------------
--- Custom Meta Data ----------------
FMS No:                   1080
Date:                     070912
Time:                     122008
Project:                  QOM
Area:                     QANAVAT
Line No:                  11
Segment No:               1
Waypoint No:              16
WGS84 Latitude:           N34.559901
WGS84 Longitude:          E050.758750
WGS84 Altitude [m]:       1717.9
Pos Solution:             GPS
Track [degree]:           272
Midpulse correction [s]:  0.00086
-------------------------------------

as you can see it has a loop that repeats the information

i need to write a C# program to extract all these kind of substrings from my txt file

"1080"
"Longitude:   E050.758750."
"Latitude :     N34.559901."
[m]: 1717.9"

is there any one who can help me in this ????

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滿滿的愛 2024-08-10 18:57:25

那些“..”部分可能是在翻译中丢失的 CR、LF 行结尾。

显而易见的答案是使用正则表达式 (RegEx),但您可能需要通过恢复行并仅从某些行中提取内容来进行一些预处理。我认为您的结果组来自 1“行”是一个条件。特别是“1080”值很容易不匹配。

当您需要正则表达式方面的帮助时请回复。


经过 Lasse 精彩的编辑后,问题变得更加清晰了。

通过查看数据,我想说您实际上并不需要 RegEx,但您可以一次处理 1 行数据并使用 line.StartsWith(...) 对它们进行分类。当您找到 StartsWith("====") 的行时,您就喜欢“记录”的结束+开始。

Those ".." sections probably are CR,LF line endings that got lost in translation somewhere.

The obvious answer is to use Regular Expressions (RegEx), but you may want to pre-process a little by restoring the lines and extract stuff from certain lines only. I gather that it is a condition that your result groups are from 1 'line'. Especially that "1080" value could be mis-matched easily.

Post back when you need help with your regexes.


After Lasse's fantastic Edit, the problem becomes a little clearer.

By looking at the data I would say you don't really need RegEx, but you can process the data 1 line at a time and classify them with line.StartsWith(...). When you find a line that StartsWith("====") you have fond the end+start of a 'record'.

ˇ宁静的妩媚 2024-08-10 18:57:25

正则表达式可能是执行此操作的好方法,但由于我不太擅长,请尝试以下操作:
我假设这就是你想要的。

    public ObjectOfMyFile ParseFile(string fileContent)
    {
        ObjectOfMyFile objectOfMyFile = new ObjectOfMyFile();
        string[] contentLines = fileContent.Split(new[] { Environment.NewLine },
                                                  StringSplitOptions.RemoveEmptyEntries);

        for (int i = 0; i < contentLines.Length; i++)
        {
            string contentLine = contentLines[i];

            if (contentLine.StartsWith("FMS No", StringComparison.OrdinalIgnoreCase))
            {
                string[] fmsNo = SplitByColon(contentLine);
                if (fmsNo.Length == 2)
                {
                    objectOfMyFile.Longitudes.Add(fmsNo[1].Trim());
                }

                continue;
            }

            if (contentLine.IndexOf("WGS84 Longitude", StringComparison.OrdinalIgnoreCase) > -1)
            {
                string[] longitudeKeyValue = SplitByColon(contentLine);
                if (longitudeKeyValue.Length == 2)
                {
                    objectOfMyFile.Longitudes.Add(longitudeKeyValue[1].Trim());
                }

                continue;
            }
        }

        return objectOfMyFile;
    }

    public string[] SplitByColon(string valueToSplit)
    {
        return valueToSplit.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
    }

    public class ObjectOfMyFile
    {
        public ObjectOfMyFile()
        {
            Longitudes = new List<string>();
            FmsNos = new List<string>();
        }
        public List<string> Longitudes { get; private set; }
        public List<string> FmsNos { get; private set; }
        // Etc...
    }
}

有一些代码重复用于检查数组中是否有第二个值,但您可以自己改进

Regex are probably a good way to do this, but since i'm not ver good at it, try this:
I'm asuming that this is what you want.

    public ObjectOfMyFile ParseFile(string fileContent)
    {
        ObjectOfMyFile objectOfMyFile = new ObjectOfMyFile();
        string[] contentLines = fileContent.Split(new[] { Environment.NewLine },
                                                  StringSplitOptions.RemoveEmptyEntries);

        for (int i = 0; i < contentLines.Length; i++)
        {
            string contentLine = contentLines[i];

            if (contentLine.StartsWith("FMS No", StringComparison.OrdinalIgnoreCase))
            {
                string[] fmsNo = SplitByColon(contentLine);
                if (fmsNo.Length == 2)
                {
                    objectOfMyFile.Longitudes.Add(fmsNo[1].Trim());
                }

                continue;
            }

            if (contentLine.IndexOf("WGS84 Longitude", StringComparison.OrdinalIgnoreCase) > -1)
            {
                string[] longitudeKeyValue = SplitByColon(contentLine);
                if (longitudeKeyValue.Length == 2)
                {
                    objectOfMyFile.Longitudes.Add(longitudeKeyValue[1].Trim());
                }

                continue;
            }
        }

        return objectOfMyFile;
    }

    public string[] SplitByColon(string valueToSplit)
    {
        return valueToSplit.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
    }

    public class ObjectOfMyFile
    {
        public ObjectOfMyFile()
        {
            Longitudes = new List<string>();
            FmsNos = new List<string>();
        }
        public List<string> Longitudes { get; private set; }
        public List<string> FmsNos { get; private set; }
        // Etc...
    }
}

Ther is some code duplication for checking there is a 2nd vlaue in the aray, but you can make that better your self

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文