Find All Text Between String A and String B, Exclusive

Find, replace, find in files, replace in files, regular expressions

Find All Text Between String A and String B, Exclusive

Postby howardg22 » Wed May 18, 2005 10:47 am

I'm sure there is a simple solution, but I cannot find it (new to RegExp). Does anyone know how to do this?

I'm trying to use a RegExp to find text between 2 strings that excludes those 2 strings. Below, I want "this" (sandwiched between [ and , ).

Line[this,that]

UE RegExp's that I've tried are:
^[+*,+
^[+^(*^),+
howardg22
 

Re: Find All Text Between String A and String B, Exclusive

Postby ASTTMan » Thu May 19, 2005 7:58 am

chadrey,

You threw me off in the description ("between 2 strings"), but I think the example using characters can be done.

Just search for the following:
(I use Unix style RE)

\[[^\[,]*,

This says search for the literal "[" character, followed by any number of characters that are not a left bracket or comma, followed by a comma.

I'm sure you can do the same thing with UE REs if you want.

If you really wanted to search for something "between 2 strings that excludes those 2 strings", I don't know how to do that.

Dave
User avatar
ASTTMan
Basic User
Basic User
 
Posts: 26
Joined: Fri Feb 18, 2005 12:00 am
Location: Arlington, TX

Re: Find All Text Between String A and String B, Exclusive

Postby howardg22 » Thu May 19, 2005 2:41 pm

Dave,

Thanks for that regexp. It works except that it selects the opening/closing characters in the match (Line[MyContent,,that] but I want Line[MyContent,that]). That is what I meant by exclusive, don't select the opening/closing characters in the match.

I've figured out the end match (ue style) [~,]+ but I'm having trouble figuring out how to match the beginning [ without actually selecting it.

Thanks for the assistance.
howardg22
 

Re: Find All Text Between String A and String B, Exclusive

Postby ASTTMan » Thu May 19, 2005 3:46 pm

chadrey,

Oh, I see what you mean. I'm afraid I don't know any way to do exactly what you ask. Now, what you want to do with this find may affect how you proceed. For example, if you want to do a replace, or even extract the found items, you could do it with one more step.

If you wanted to replace the string, you could put parens around the desired section, such as: "\[([^\[,]*),". In the replace string you could refer to the contents as "\1", which would be the string between the delimiters.

If you want to extract the contents into a list, you can check "List Lines Containing String" in the find dialog and paste them into a file. It would then be a simple step to get rid of the delimiters. (Replace "\[([^\[,]*)," with "\1")

I'm not sure how else to approach it. Maybe if you could give some more detail on what you want to do once you have found the string, I'd have some other ideas.

Dave
User avatar
ASTTMan
Basic User
Basic User
 
Posts: 26
Joined: Fri Feb 18, 2005 12:00 am
Location: Arlington, TX

Re: Find All Text Between String A and String B, Exclusive

Postby howardg22 » Fri May 20, 2005 9:35 am

Maybe a single RegExp cannot do this? All of the documentation mentions "Match" and I'm looking for a "Find position, then start matching".

I put it in a macro.

InsertMode
ColumnModeOff
HexOff
UnixReOff
ClearClipboard
Key Ctrl+HOME
Find "["
Key RIGHT ARROW
SelectWord
IfSel
Copy
EndIf

Thanks for the help.
howardg22
 


Return to Find/Replace/Regular Expressions