Conditional match of the last HTML tag

I have two lines

<EM>is <i>love</i></EM>,<PARTITION />

      

and

<EM>is <i>love</i>,<PARTITION />

      

I want the regex to match the second line exactly, but not have to match the first. Please, help.

Note. Everything can change except for the EM and PARTITION tags.

0


source to share


4 answers


luckily after going through all this and doing a lot of research on the subject, I found the correct regexx .......... heres foor you all ... thanks for everyone who helped

<EM>\w*\s*\W*\S*[^\(</EM>)]<PARTITION[ ]/>

      



commits the second line, but leaves the first. The only problem I ran into was to deny the combination </EM>

I did with a backslash in front of the group, this nullifies the entire string, rather than passing characters seperatly ....

0


source


If you want to completely match a string if it does not contain a specific substring, use a regex to match the substring and return the whole string if the regex does not match. You didn't say what language you are using, but you tagged your .NET question, so here goes C #:

if (Regex.IsMatch(subjectString, "</EM>")) {
    return null;
} else {
    return subjectString;
} 

      

Since it's just a letter of literal text, you don't even need to use a regular expression:

if (subjectString.Contains("</EM>")) {
    return null;
} else {
    return subjectString;
} 

      



In a situation where all you can use is a regex, try this:

\A((?!</EM>).)*\Z

      

A regex-only solution will be much less efficient than the code examples above.

+1


source


I don't think you are asking the right question. This regex exactly matches the second line, not the first:

/ ^ <EM> is <i> love <\ / i>, <PARTITION \ /> $ /

But obviously you want to match the string class, not just the second string ... right? Define the class of strings you want to match and you can be 1 step closer to getting the regex you want.

0


source


^<EM>(?:(?<!</EM>).)*<PARTITION />$

      

works. But it depends on the target language, like JavaScript does not support search assertions ...

A simpler solution is to use ^<EM>.*<PARTITION />$

and just check if there is a line in the text </EM>

: I believe REs are powerful and should have, but I'm not trying to do everything in just one expression ... :-)

0


source







All Articles