Eclipse regex find and replace - regex

I want to replace the below statement
ImageIcon("images/calender.gif");
with
ImageIcon(res.getResource("images/calender.gif"));
Can anyone suggest a regex to do this in eclipse.Instead of "calender.gif" any filename can come.

You can find this pattern (in regex mode):
ImageIcon\(("[^"]+")\)
and replace with:
ImageIcon(res.getResource($1))
The \( and \) in the pattern escapes the braces since they are to match literally. The unescaped braces (…) sets up capturing group 1 which matches the doublequoted string literal, which should not have escaped doublequotes (which I believe is illegal for filenames anyway).
The […] is a character class. Something like [aeiou] matches one of any of the lowercase vowels. [^…] is a negated character class. [^aeiou] matches one of anything but the lowercase vowels.
The + is one-or-more repetition, so [^"]+ matches non-empty sequence of everything except double quotes. We simply surround this pattern with " to match the double-quoted string literal.
So the pattern breaks down like this:
literal( literal)
| |
ImageIcon\(("[^"]+")\)
\_______/
group 1
In replacement strings, $1 substitutes what group 1 matched.
References
regular-expressions.info
Character Class, Repetition, Brackets
Examples/Programming Constructs - Strings - has patterns for strings that may contain escaped doublequotes

Ctrl-F
Find: ImageIcon\("([^\"]*)"\);
Replace with: ImageIcon(res.getResource("\1"));
Check Regular Expressions checkbox.

Related

modify regex to include the backslash and single quote (so that backslash behaves as an escaping character)

I have the following string:
arg1('value1') arg2('value '')2') arg3('value\'3')
The regex to extract the value looks like:
boost::regex re_arg_values("('[^']*(?:''[^']*)*')");
Now this regex is not able to extract 'value\'3'. How can I modify the regex to consider \' inside the parenthesis as well.
FYI. The value can contain spaces, special characters, and also tabs. The code is in CPP.
Thanks in advance.
boost::regex re_arg_values( "\('([^'\\]|''|\\.)*'\)" );
The \(' and '\) match the bounds.
The (, |, and )* for matching any of the given patterns
The [^'\\] matches normal characters.
The '' matches a pair of single quotes.
The \\. matches any escaped character (including stacked backslashes).

Pattern for apostrophe inside quotes

I am looking for a pattern that can find apostrophes that are inside single quotes. For example the text
Foo 'can't' bar 'don't'
I want to find and replace the apostrophe in can't and don't, but I don't want to find the single quotes
I have tried something like
(.*)'(.*)'(.*)'
and apply the replace on the second matching group. But for text that has 2 words with apostrophes this pattern won't work.
Edit: to clarify the text could have single quotes with no apostrophes inside them, which should be preserved as is. For example
'foo' 'can't' bar 'don't'
I am still looking for only apostrophes, so the single quotes around foo should not match
I believe you need to require "word" characters to appear before and after a ' symbol, and it can be done with a word boundary:
\b'\b
See the regex demo
To only match the quote inside letters use
(?<=\p{L})'(?=\p{L})
(?<=[[:alpha:]])'(?=[[:alpha:]])
(?U)(?<=\p{Alpha})'(?=\p{Alpha}) # Java, double the backslashes in the string literal
Or ASCII only
(?<=[a-zA-Z])'(?=[a-zA-Z])
You can use the following regular expression:
'[^']+'\s|'[^']+(')[^' ]+'
it will return 3 matches, and if capture group 1 participated in the word, it will be the apostrophe in the word:
'foo'
'can't'
'don't'
demo
How it works:
'[^']+'\s
' match an apostrophe
[^']+ followed by at least one character that isn't an apostrophe
' followed by an apostrophe
\s followed by a space
| or
'[^']+(')[^' ]+'
' match an apostrophe
[^']+ followed by at least one character that isn't an apostrophe
(') followed by an apostrophe, and capture it in capture group 1
[^' ]+ followed by at least one character that is not an apostrophe or a space
' followed by an apostrophe

Regex to Match All Whitespace After Word

I have strings like this:
"2015/08/this filename has whitespace .jpg"
I need to match the whitespace characters in those strings. They will all have "2015/08/ and will end with ".
I'm using Sublime Text 2 to search and replace in a SQL DB dump. I'm at a loss on how to do the match. I know I can match whitespace with \s, but I have no clue how to contain to those groups.
As per my comment, this expression should work for a string that has the same number of opening/closing double quotes:
\s+(?=(?:(?:[^"]*"){2})*[^"]*"[^"]*$)
See demo here. The look-ahead is checking for an odd number of double quotes until the end of file.
Another approach is to define the boundary with \G and trim the beginning of the match with \K:
(?:"\d{4}\/\d{2}\/|(?!^)\G)[^"\s]*\K\s(?=[^"]*")
See demo
The regex finds a match:
(?:"\d{4}\/\d{2}\/|(?!^)\G) - when a substring starts with numbers like 2015/12/ or after a successful match
[^"\s]*\K - matches all characters that are not whitespace or " and omits them due to \K operator
\s - here it matches a whitespace symbol
(?=[^"]*") - a look-ahead checking we are presumably inside double quotes.
Replacing the spaces with, say, %20 results in:

Why test for matching \. if testing for matching anything but a specific character?

http://perldoc.perl.org/Text/Balanced.html says:
The extract_delimited function formalizes the common idiom of extracting a single-character-delimited substring from the start of a string. For example, to extract a single-quote delimited string, the following code is typically used:
($remainder = $text) =~ s/\A('(\\.|[^'])*')//s;
$extracted = $1;
\\. (which is '\.') seems to be covered by ([^'])*. Why is \\.| in there? Why not use s/\A('[^']*')//s?
'(\\.|[^'])*' would match an escaped quote or escaped any special character like '\'\"' where '[^']*' won't match '\'', that's the main difference.
'(\\.|[^'])*'
' matches a single quotes.
\\. Match any escaped character. If there is no backslash then the control shift to the negated character class [^']. So this would greedily match all the escaped characters.
' Match a single quote.

How to match an apostrophe (') unless it is escaped (\')?

Is it possible to construct a regular expression for this? If so, I'd appreciate if someone shows how.
Use this regular expression:
(?<!\\)'
It means match all apostrophe chars not preceeded by a backslash (the backslash is escaped itself because it is a special char for regexps)
If you are using the .NET regex engine or another engine that can handle indefinite-length lookbehind assertions, then use
(?<=(?<!\\)(?:\\\\)*)'
That makes sure that there is an even number of backslashes before the apostrophe.
Explanation:
(?<= # Assert that the following regex matches before the current position:
(?<!\\) # No backslash before...
(?:\\\\)* # ... an even number of backslashes.
) # (End of lookbehind assertion)
' # Match an apostrophe.
If your regex engine can't handle that, you'll need to make the (even number of) backslashes part of the match and account for them later:
(?<!\\)((?:\\\\)*)'
Now $1 (or \1) will contain the matched backslashes, so you can replace the result by \1\\' or $1\\', depending on the details of the QRegExp implementation.