eclipse search and replace with wildcard - regex

I am trying to remove all occurances of id="someId" where someId is different in every case. I have tried to use this syntax: (id="\w+\"\(\)) because it's used in similar situations by other posters. But I don't understand the syntax and of course it doesn't work.
Could someone tell me why this syntax is incorrect and maybe point me to a resource that explains the syntax?

Check "Regular expressions" check box.
The expression is
id\s*=\s*"[^"]+"
\s* means space, * means zero or more
[^"] means anything but quote, + means one or more
To capture the string use "([^"]+)" and $1 in the replace with field.
More info at http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

Related

Confusion regarding the *? regular expression operator

So I want to search a string, using the below regular expression:
border-.*\.5pt
to find all border-top, border-bottom, etc CSS properties in a file with a border thickness of .5pt. It generally works great, but it's too greedy.
For example all of the below comes back as a single match:
border-top:solid #1F497D .5pt;border-bottom:solid #1F497D .5pt
I want those two CSS properties to be two separate matches.
So I tried to modify my regular expression to:
border-.*?\.5pt
Using ? to make it non-greedy. However, after that modification, nothing matches.
Can anyone explain why I see this behavior? What am I missing?
(If it's worth knowing, I'm using Microsoft Expression Web's 'find with regular expressions' when doing this search.)
There is no one "regular expression" language. While there are broad commonalities, details differ from implementation to implementation. Many regexes use - to be the non-greedy "0 or more", others use *?. Apparently Microsoft Expression Web uses #.
In short, regexes can differ, so you'll often need to RTM for the one you're using to find its range of capabilities and detailed syntax (i.e. support for alteration/backtracking/etc., grouping character, set shorthand, etc.)
.*? is the badest, so to say "antipattern" for Regular Expressions. It is commonly used as a "Match-something-until-the-string-i-want" Pattern - but it isn't.
Especially when combining multiple .*? within ONE pattern, it may lead to very wrong and unexpected results.
For your Case - as stated in the comments - It works. (Maybe you did something wrong?)
However, it is ALWAYS a good idea to be more specific, when generating a regex pattern.
ALWAYS KEEP IN MIND that .*? can be ANYTHING. Also Stuff you really don't want to match!
In your example, i would use something like this: border-(?:[^:]+):\s*(?:[^\s]+)\s+(?:\#[a-fA-F0-9]{6})\s+(?:\d*(?:\.\d+)?)pt;?
It is more specific, but matches the given Requirements, ignores all whitespaces that dont make sence, and even matches border widths, regardles if they are written as .2, 3 or 4.1. If you remove the ?: from the single match Groups you can also match every single attribute, if required. : Position, Border type, Color and thickness.
The pattern border-([^:]+):\s*([^\s]+)\s+(\#[a-fA-F0-9]{6})\s+(\d*(?:\.\d+)?)pt;? with your string border-top:solid #1F497D .5pt;border-bottom:solid #1F497D .5pt will match:
First Match:
1.top
2.solid
3.#1F497D
4..5
Second Match:
1.bottom
2.solid
3.#1F497D
4..5

Find / Replace functionality that allows for boundary replacements instead of expressions

Apologies in advance for the confusing title. My issue is as follows, I have the following text in about 600 files:
$_REQUEST['FOO']
I would like to replace it with the following:
$this->input->post('FOO')
To clarify, I am matching against the following:
$_REQUEST any number of A-Za-z\d followed by a ]
and replacing it with:
$this->input->post( the alphanumeric word from above followed by a )
Or in general:
Anchor token TEXT TO KEEP end anchor token
This differs from standard find/replace as I want to retain text inside of two word boundaries.
Is this functionality present in any text editors (Eclipse,np++,etc). Or am I going to need to write some type of program to parse these 600 files to make the replacement?
s/\$__REQUEST\[(.*?)]/$this->input->post(\1)/
The .*? will match everything from [ to the first ] rather than the last although it's unlikely that it will matter in this case.
By the way the PHP superglobal is $_REQUEST rather than $__REQUEST
You can do this in Notepad++ using regular expressions. Replace
\$_REQUEST\['([^']*)'\]
with
$this->input->post('$1')
If you ever have double-quotes too, you can do use a more complex expression to handle both cases, though I'm not sure Notepad++ supports backreferences; replace
\$_REQUEST\[(['"])(.*?)\1\]
with
$this->input->post($1$2$1)
Note that I've reverted to using #ExplosionPills' suggested (.*?) hereā€”it may be better, actually.

regular expression for find replace modification

I wanted to use regular expressions in eclipse to adept code to a software update.
instead of
{$CFG->prefix}example1.xy
the code needs to be:
{example1}.xy
to work.
another example would be:
{$CFG->prefix}example2.foo
>
{example2}.foo
constant parts are : {$CFG->prefix}; .
i tried the following (i used whitespaces to make reading easier):
Find: \{\$CFG-\>prefix\} ([a-z]|[0-9])* \. ([a-z]|[0-9])*
Which will find the requested String. I struggle in replacing it.
i can use ,/1to store the result of the regex and use it in the replacement (right?) but i am not sure how i can modify/manipulate this result.
thanks for any help.
You can try
\{\$CFG-\>prefix\}([a-z0-9]*)\.
and replace with
{\1}.
I am not sure why you do have the whitespaces in your regex, I removed them.
the quantifier * should be inside your group, otherwise you will have only the last matched character in \1 and not the complete word.
Since you don't want to replace the last part, you don't need to match and replace it.
Try the following search and replace :
Find: \{\$CFG->prefix\}([a-z0-9]*)\.([a-z0-9]*)
Replace with : {\1}.\2
Using the above the following :
BECOMES
Here is a quick screen-cast to show this in action.
Changes made to the OP's Find reg-ex
In order to get the above find-replace to work, I had to make the following changes to the OP's find expression :
Removed whitespaces.
Moved the Greedy Match modifier inside the groups : i.e. ([...]*) instead of ([...])*
Corrected the character set : i.e [a-z0-9] instead of [a-z]|[0-9]
Introduced another Group which captures the part after the period. This however is not strictly needed but may be useful in some scenarios.

Regex exclude bracket symbol

I am trying to use RegEx to find all cells in OO-calc with : [RB]Condition=New
the RB part is necessary, as other cells may get caught if it is not specified.
the RegEx that would work would be : [RB]Condi.*
however the brackets are read as a part of the expression, is there any way to fix this?
Use an escape character to treat it as an actual character. For example \[ would find all [. I think the Regex you're looking for is \[RB\]Condition=New*. The original Regex you had would find RCondition=New and BCondition=New.
*I'm not familiar with OO-calc, so I'm not entirely sure I understand what you mean.

Capture followed by Digits: Replace Syntax? (Dreamweaver)

When you address a regex capture, things can get tricky when digits follow the capture. In PCRE, I can write
${1}000
to substitute the capture of Group 1 followed by three zeroes.
Does anyone know the equivalent syntax in Dreamweaver replace operations, if any?
If we had a series of "A"s instead of zeroes, we could use:
$1AAAA
But these:
$10000
${1}0000
do not work.
I believe the regex flavor is ECMAScript. Just cannot find the information.
This may not be addressed in the syntax. If so, that would be good to know.
Thank you!
Edit: I should add that this is not matter of life and death as I have a number of grep tools at my fingertips. I would just like to know.
Dreamweaver's regular expression find and replace is supposed to be based on JavaScript's implementation of RegExp. You should be able to just use $1000 in the replacement text. However, like you've found, the replacement groups ($ + group number) are not properly recognized when the replacement text has digits immediately after the grouping token.
FWIW: I've logged a bug on this at http://adobe.ly/DWwish