I’m sure this question has been asked somewhere (maybe on here), but I couldn’t find any information and that may be due to the fact that I’m not sure how to describe it exactly.
Essentially, I’m looking do an advanced find and replace. The replace I’m sure I can figure out when it comes to it, but right now I can’t get the find to work.
In an application, there are a lot of htm files being loaded. The user chooses a file and performs an action with it. After this action is done, I want the system to scan the file one more time to ensure there aren’t issues. For example, this string could be present in the htm file:
<?strange_tag_start
name="var_value" ?>Name<?strange_tag_end ?>
And, yes, it could be broken across lines like this. The above isn’t a problem unless this happens:
<?strange_tag_start
name="var_value" ?><?strange_tag_start
name="var_value" ?>Name<?strange_tag_end ?><?strange_tag_end ?>
The line breaks could be different. What I want to do is search the document for strings that contain <?strange_tag then contain <?strange_tag_end ?>. After if it find those, I want to check within the string whether there is either another <?strange_tag_start or another <?strange_tag_end ?>.
I initially tried reading the file and getting every index of the specific values then attempting to compare them. However, the following could be present in the file, and these are perfectly ok, but the system finds them and flags them for me:
<?strange_tag_start
name="var_value" ?>Name<?strange_tag_end ?> There is other text here
and some more text on another line. Then this <?strange_tag_start name="var_value"
?>Name<?strange_tag_end ?> is present.
What it boils down to is a system (such as is present in some applications) where the beginning of a string is specified, the end of the string is specified, and then the system checks to see if it contains a string.
If this doesn’t make sense or you need more clarification, I can do that.
UPDATE
Let me clarify with this. I have the following multiline string:
I want to preserve<?start_foo
bar="value" ?> the content
<?start_baz qux="value" ?>Name
<?end-baz_qux ?>that is between weird tags.
I want to find <?start_foo bar="value"
I also want to find <?end-baz_qux ?> (Note: There could be two of these right next to each other.)
After finding those, I want to check if inside that string is another <?start_foo bar= (Note: The “value” in that tag could be different as well.)
Then I want to remove the middle content that is not suppose to be there so I end up with:
I want to preserve<?start_foo
bar="value" ?> the content
<?end-baz_qux ?>that is between weird tags.
Here’s another example to hopefully make it clearer:
Back <?rh-udv_start name="ctrl_btn" ?><?rh-udv_start name="ctrl_btn"
?>button<?rh-udv_end ?><?rh-udv_end ?> to
After doing the search, I should end up with this:
Back <?rh-udv_start name="ctrl_btn" ?>button<?rh-udv_end ?> to
Essentially, I’m looking for a way to say:
- Find a string that “begins” (misleading as the “begin” could be in the middle of the string) with VALUE_X.
- If found, find VALUE_Y after it (this should always be found if there is a VALUE_X).
- Check after the VALUE_Y to see if there is another VALUE_Y.
- Check inside the string of VALUE_X through VALUE_Y to see if contains another VALUE_X.
- If there is another VALUE_X, delete it. If there is a VALUE_Y immediately after VALUE_Y, delete the second VALUE_Y.
I believe that
Would work to find the tags in most regex flavors (including Visual Studio’s – not sure which you are using).
If you also want to replace the content between the strange tags, then could you please show us a more realistic example? It’s vital to know exactly what you are trying to match (or some very close approximation) in order to provide the correct regex. For example
is very different from
which is different from
Etc.
UPDATE
Based on your comment below, I’m going to assume that you have a document that looks like this:
And that you want the result to be:
I will also assume that you are using the .NET regex assembly (rather than the regex that is built-in to Visual Studio. Yes, they are different.)
If that’s the case, then you can use something like this:
l_inputbecomes:UPDATE 2
In response to your question update, try this code:
l_inputbecomes:It simply looks for a repeating tag and deletes it. For example:
the first tag will be deleted, leaving only:
Likewise with the end tags. The code will not tolerate space or content between tags (it will not delete either tag in that case). Feel free to work with the example until you have what you want.