简体   繁体   中英

regular expression for find and replace

I've got strings like:

('Michael Herold','Michael Herold'),

but I need to remove the last parts so I end up with:

('Michael Herold'),

I'm still new to Regular Expressions so they confuse me. I'm using Notepad++.

find: \('([^']*)','\1'\) 
Replace: ('\1')

So the actual function you use will depend on the language. Notepad++ is a text editor, not a language.

The regular expression that you will want will be ",'Michael Herold'" and you'll replace any matches with "", the empty string.

So in PHP for example, you'll have

    $source = "('Michael Herold','Michael Herold')";
    $pattern = "/(,'Michael Herold')+/";
    $newString = $preg_replace($pattern, $source, ""); 

Do the equivalent in whatever language you use.

I'm not sure what flavor of regular expressions Notepad++ uses, but try replacing this expression:

\('([^']*)','\1'\)

with this one:

('$1')

The \1 matches whatever was found in the first set of single quotes ( Michael Herold in your example), and $1 is replaced with that same string. (Try \1 if $1 doesn't work in Notepad++.)

See it in action here .

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