Chani Poz
Chani Poz

Reputation: 1463

One Regex for 2 different text c#

I have 2 strings. and I need one Regex for both.

s1="The 8481D provides extraordinary accuracy, stability, and lower SWR.";

s2="<li>Complete with case and 9V battery</li><div id='warranty'><img src='1yr.gif'>";

I need to get all characters of s1 and the characters of s2 till the characters: <div id='warranty'>

so, it will be:

    s1="The 8481D provides extraordinary accuracy, stability, and lower SWR.";

    s2="<li>Complete with case and 9V battery</li>";

I thought of: .+?(?<=<div id="warranty">) but I got just the s2 string, also .+?(?<=<div id="warranty">|\.) didn't work, I got s1, but got too much characters in s2.

Upvotes: 0

Views: 92

Answers (2)

Mijalko
Mijalko

Reputation: 529

.+?(?=<div\sid='warranty'>|\.)

or if you want to include and dot regex will be:

^.+?(?=<div\sid='warranty'>|$)

Upvotes: 2

Ria
Ria

Reputation: 10347

Simplest way to do this in C# is using IndexOf and Substring methods (if you not insist on Regex):

static String GetValidString(String inputString)
{
    int end = inputString.IndexOf("<div id='warranty'>");
    if (end == -1)
        end = inputString.Length;
    return inputString.Substring(0, end);
}

Upvotes: 1

Related Questions