Autor Beitrag
vreden123
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 95
Erhaltene Danke: 2



BeitragVerfasst: Mo 08.03.10 10:10 
Hallo ich habe eine Frage zu der Regex Klasse.

Meine Seite hat folgenden Quelltext:

treffer>1<treffer
treffer>2<treffer
treffer>3<treffer
treffer>4<treffer
treffer>4<treffer
treffer>5<treffer


Und ich folgendem C# Code habe:

ausblenden C#-Quelltext
1:
2:
3:
4:
glink = "Adresse zur Seitel";
arr = client.DownloadString(glink);
Regex r = new Regex("treffer>(.*)<treffer");
string treffer = r.Match(arr).Groups[1].Value;


Dann hat die Variable "treffer" den wert 1.

Kann man das mit der Regexklasse so machen das die Variable treffer nicht den Wert 1 hat sondern den Wert 2. Also das das zweite gefundene Ergebnis in der Variable treffer geschrieben wird?

PS: Google hab ich schon durchsucht aber nichts gefunden.

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
Moderiert von user profile iconChristian S.: Topic aus ASP.NET und Web verschoben am Mo 08.03.2010 um 12:19
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Mo 08.03.10 10:41 
Hallo,

mit Matches bekommst du jeden Treffer bzw. eine MatchCollection für dein Pattern zurück. Diese kannst du indizieren.

Gruß Daniel

PS: Code als Code mit  und-Tag deklarieren und mit (?<group_name><pattern>) kannst du benannte Groupen erstellen.

Bsp:
ausblenden C#-Quelltext
1:
2:
3:
4:
foreach(var item in Regex.Matches("trffer>(?<value>.*?)<treffer"))
{
   Console.WriteLine(item.Group["value"].Value);
}
vreden123 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 95
Erhaltene Danke: 2



BeitragVerfasst: So 14.03.10 14:27 
Das habe ich jetzt soweit gemacht. Aber es kommt immer noch eine Fehlermeldung:

Für das nicht statische Feld, die Methode oder Eigenschaft
"System.Text:regularExpressions.Regex.Matches(string)" ist ein Objektverweis erforderlich.

Was heist das?
Ich bin leider noch nicht so erfahren und möchte es lernen.
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: So 14.03.10 14:34 
Zeig doch mal bitte Deinen Quellcode :)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
vreden123 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 95
Erhaltene Danke: 2



BeitragVerfasst: So 14.03.10 15:00 
Den Oben genannten fehler bin ich jetzt los.

Ich poste als erstes man den Quellcode:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (WebClient client = new WebClient())
            {
                string glink;
                string arr;
                client.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                glink = "http://wbsucher.square7.ch/neu/test.html";
                arr = client.DownloadString(glink);
                
                Regex r = new Regex("treffer>(.*)<treffer");
                
                foreach (var item in r.Matches("treffer>(?<value>.*?)<treffer"))
                {

                    label4.Text = item.Groups[1].Value;
                }   
            }
        }
    }
}




Bei label4.Text = item.Groups[1].Value;
mekrt er das:
"objekt" enthält keine Definition für "Groups", und es konnte keine Erweiterungsmethode "Groups" gefunden werden, die ein erstes Argument vom Typ "objekt" akzeptiert. (Fehlt eine Using-Direktive oder ein Assemblyverweis?)

Kann mir da jemand helfen?

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: So 14.03.10 15:07 
Da habe ich mich schon so oft drüber geärgert ... MatchCollection implementiert nur die "alten" Interfaces ICollection und IEnumerable, aber z.B. nicht IEnumerable<Match>. Du musst den Typ in der foreach-Schleife also explizit angeben:

ausblenden C#-Quelltext
1:
foreach (Match item in r.Matches("treffer>(?<value>.*?)<treffer"))					

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".