I want to do a bulk insert of a space between certain JavaScript keywords and the opening parenthesis immediately following them.
The regex I was going to use, is:
(if|function|instanceof|return)\(
But I can't replace it with a fixed string like
this (
since it would overwrite the individual keywords.
How do I find just the paren in that regex, such that I can replace it with:
[blank space](
I'd want to use a single regex instead of one search expression for each individual keyword.
Use $N to refer to the string matched by the Nth capture group:
newstr = str.replace(/(if|function|instanceof|return)\(/g, '$1 (');
Related
I want to use a regular expression in VBA to do a search, capture part of it, and then use that part in the replacement. For example, I want to run the search and replace on these lines:
(a4a)
(aHa)
And get a result of:
(b4b)
(bHb)
How do I capture the 2nd character and use it again in the replacement?
In VBA, capture parts of the search with parenthesis () and use them in replacement with $ and the number of the capture occurrence. Note, normal parenthesis need to be escaped, which is the opposite of vim.
So in this case:
searchPattern = "\(a(.)a\)"
replacement = "(b$1b)"
I have something in a text file that looks like '%r'%XXXX, where the XXXX represents some name at the end. Examples include '%r'%addR or '%r'%removeA. I can match these patterns using regex '%r'%\w+. What I would like to replace this with is '{!r}'.format(XXXX). Note that the name has to stay the same in the replace so I'd get '{!r}.format(addR) or '{!r}.format(removeA). Is there a way to replace parts of the matched string in this way while retaining the unknown variable name pulled out with \w+ in the regex search?
I'm specifically looking for a solution using the find and replace features in Notepad++.
You can use
'%r'%(\w+)
and replace with '{!r}.format\(\1\)
The '%r'%(\w+) pattern contains a pair of unescaped parentheses that create a capturing group. Inside the replacement pattern, we use a \1 backreference to restore that value.
NOTE: The ( and ) in the replacement must be escaped because otherwise they are treated as Boost conditional replacement pattern functional characters.
See more on capturing groups and backreferences.
Search on:
'%r'%(XXXX)
Replace with:
Whatever You like \1
\1 will match the first set of grouping parentheses.
I have application where few labels are written like
ui-label-Display Not Masked
Now I want to replace it by
ui-label-Display_Not_Masked
so i have written search regex by
ui-label-(\w+ )*
This searches all expression but I am not able to create a expression to replace this text as required.
I have written one regex
$1_
which replaces
ui-label-Display Not Masked
by
ui-label-Display Not_Masked
This cannot be done with a single regex in a single iteration.
You have two choices:
Replace (ui-label-\w+) (note the space at the end) with $1_ until it no longer matches anything.
Make a looong regex with as many capture groups as necessary, i.e. (ui-label-\w+) (?:(\w+)(?: (\w+))?)? and replace with $1_$2_$3.
I've been struggling to find a way to replace spaces with dashes in a string but only spaces that are within a particular part of the string.
Source:
ABC *This is a sub string* DEF
My attempt at a regular expression:
/\s/g
If I use the regular expression to match spaces and replace I get the following result:
ABC-*This-is-a-sub-string*-DEF
But I only want to replace spaces within the text surrounded by the two asterisks.
Here is what I'm trying to achieve:
ABC *This-is-a-sub-string* DEF
Not sure why type of regular expressions I'm using as I'm using the find and replace in TextMate with Regular Expressions option enabled.
It's important to note that the strings that I will be running this regular expression search and replace on will have different text but it's just the spaces within the asterisks that I want to match.
Any help will be appreciated.
To identify spaces that are surrounded by asterisks, the key observation is, that, if asterisks appear only in pairs, the spaces you look for are always followed by an odd number of asterisks.
The regex
\ (?=[^*]*\*([^*]*\*[^*]*\*)*[^*]*$)
will match the once that should be replaced. Textmate would have to support look-ahead assertions for this to work.
s/(?<!\*)\s(?!\*)(?!$)/-/g
If TextMate supports Perl style regex commands (I have no experience with it all, sorry), this is a one-liner that should work.
try this one
/(?<=\*.*)\s(?=.*\*)/g
but it won't work in javascript if you want to use it in it, since it uses also lookbehind which is not supported in js
Try this: \s(\*[^*]*\*)\s. It will match *This is a sub string* in group 1. Then replace to -$1-.
Use this regexp to get spaces from within asterisks
(.)(*(.(\ ).)*)(.)
Take 4th element of the array provided by regex {4} and replace it with dashes.
I find this site very good for creating regular expressions.
It depends on your programming language but in many of them you can use lambda functions with your regular expression replacement statements and thereby perform further replacement on substrings.
Here's an example in Python:
string = "ABC *This is a sub string* DEF"
import re
new_string = re.sub("\*(.*?)\*", lambda x: '*' + x.group(1).replace(" ", "-") + '*', a)
That should give you ABC *This-is-a-sub-string* DEF.
Given a string of identifiers separated by :, is it possible to construct a regular expression to extract the unique identifiers into another string, also separated by :?
How is it possible to achieve this using a regular expression? I have tried s/(:[^:])(.*)\1/$1$2/g with no luck, because the (.*) is greedy and skips to the last match of $1.
Example: a:b:c:d:c:c:x:c:c:e:e:f should give a:b:c:d:x:e:f
Note: I am coding in perl, but I would very much appreciate using a regex for this.
In .NET which supports infinite repetition inside lookbehind, you could search for
(?<=\b\1:.*)\b(\w+):?
and replace all matches with the empty string.
Perl (at least Perl 5) only supports fixed-length lookbehinds, so you can try the following (using lookahead, with a subtly different result):
\b(\w+):(?=.*\b\1:?)
If you replace that with the empty string, all previous repetitions of a duplicate entry will be removed; the last one will remain. So instead of
a:b:c:d:x:e:f
you would get
a:b:d:x:c:e:f
If that is OK, you can use
$subject =~ s/\b(\w+):(?=.*\b\1:?)//g;
Explanation:
First regex:
(?<=\b\1:.*): Check if you can match the contents of backreference no. 1, followed by a colon, somewhere before in the string.
\b(\w+):?: Match an identifier (from a word boundary to the next :), optionally followed by a colon.
Second regex:
\b(\w+):: Match an identifier and a colon.
(?=.*\b\1:?): Then check whether you can match the same identifier, optionally followed by a colon, somewhere ahead in the string.
Check out: http://www.regular-expressions.info/duplicatelines.html
Always a useful site when thinking about any regular expression.
$str = q!a:b:c:d:c:c:x:c:c:e:e:f!;
1 while($str =~ s/(:[^:]+)(.*?)\1/$1$2/g);
say $str
output :
a:b:c:d:x:e:f
here's an awk version, no need regex.
$ echo "a:b:c:d:c:c:x:c:c:e:e:f" | awk -F":" '{for(i=1;i<=NF;i++)if($i in a){continue}else{a[$i];printf $i}}'
abcdxef
split the fields on ":", go through the splitted fields, store the elements in an array. check for existence and if exists, skip. Else print them out. you can translate this easily into Perl code.
If the identifiers are sorted, you may be able to do it using lookahead/lookbehind. If they aren't, then this is beyond the computational power of a regex. Now, just because it's impossible with formal regex doesn't mean it's impossible if you use some perl specific regex feature, but if you want to keep your regexes portable you need to describe this string in a language that supports variables.