简体   繁体   中英

Regex: Find/Replace All Substrings Without a Given String Before?

I need to find all strings without a given string before it.

For Instance:

Find: "someValue"

**All results with "function(" before them should be ignored

The Visual Studio regular expression would find this :

value = someValue

And Ignore something looking like this :

function(someValue)

What is the best way to go about this?

Thanks for the help!

你可以使用负面的后视:

(?<!function\()someValue

You said

The Visual Studio regular expression would find this:

Combined with the title of your question, that makes me think you're trying to do something in a search and replace dialog in Visual Studio, rather than using a regex in an application.

If that's the case, then I think you might be out of luck; Visual Studio's regular expressions aren't very powerful, and they have a rather odd syntax that doesn't seem to be used anywhere else!

My advice would be to either use a different text editor, or use the regex described by SilentGhost in a .NET application (or PowerShell script) to do the replacement for you. When I need to do regex stuff in an editor and Studio doesn't cut it, I tend to use TextPad . It's not very pretty, but it's powerful and has great macro support.

Incidentally, if you want to use PowerShell to do it, this will search foo.js , and copy the output to fooNew.js :

(get-content D:\junk\foo.js) -replace
    '(?<!function\()someValue', 'someOtherValue' > D:\junk\fooNew.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM