I'm looking for a grep for JavaScript function names including parenthesis.
Example: myfunction()
I need it to ignore jQuery events like .on(), .click(), .width() as well. So basically "get word before parenthesis and parenthesis but not if there is a . before the word". I tried different stuff like ([\l\u]()) but i can't get my head around. Any Ideas?
To match a phrase where something may not appear "before" it, use a negative lookbehind:
(?<!\.)\b([\l\u]+\(\))
One possible problem in your own attempt may have been that you only test a single character before the parentheses; another may have been you did not escape the (literal!) parentheses.
This matches myfunction() and even myFunction(), but nothing more. A larger commonly used set of characters in function names would be:
(?<!\.)\b([_\l\u]\w*\(\))
Note that this ignores any and all arguments that may appear inside the parentheses (as per your specification, or so it would seem). Unfortunately, that is not easily added since you can not match "matching parentheses" with a single GREP.
Related
I'm fairly new to regex, and I can't seem to get this working:
I did some changes in one of my projects and thus I had to change variable names in multiple files. The variables were named as their class (variable lower case, Class uppercase; I know this was not good practice :D) and this confused me, so I replaced them with getters.
I want the regex to find every variable that:
was at the beginning of the line or just had whitespaces before it
was lowercase at the beginning
had a dot after it (because a property of it was used)
For example: song.data should turn to getSong().data while Song.data or this.song or even this.song.data should have stayed the same.
So far I got this regex to work: /^(song)/mg.
My problem now is, that most of my lines are beginning with white spaces (tabs) because they are in funtion bodies and I can't find a regex which accepts tabs at the beginning, but doesn't delete them whilest replacing. I hope this makes any sense for some of you ^^
PS: I already replaced all the names by hand, but now I'm curious to find out how it WOULD HAVE worked with regex
you can use a positive lookahead/lookbehind that a patern precedes or follows a match without including it in the match
(?<=PATERN)MATCH
Example
I'm trying to find all pages which contain words "text1" and "text2".
My regex:
text1(.|\n)*text2
it doesn't work..
If your IDE supports the s (single-line) flag (so the . character can match newlines), you can search for your items with:
(text1).*(text2)|\2.*\1
Example with s flag
If the IDE does not support the s flag, you will need to use [\s\S] in place of .:
(text1)[\s\S]*(text2)|\2[\s\S]*\1
Example with [\s\S]
Some languages use $1 and $2 in place of \1 and \2, so you may need to change that.
EDIT:
Alternately, if you want to simply match that a file contains both strings (but not actually select anything), you can utilize look-aheads:
(?s)^(?=.*?text1)(?=.*?text2)
This doesn't care about the order (or number) of the arguments, and for each additional text that you want to search for, you simply append another (?=.*?text_here). This approach is nice, since you can even include regex instead of just plain strings.
text0[\s\S]*text1
Try this.This should do it for you.
What this does is match all including multiline .similar to having .*? with s flag.
\s takes care of spaces,newlines,tabs
\S takes care any non space character.
If you want the regex to match over several lines I would try:
text1[\w\W]*text2
Using . is not a good choice, because it usually doesn't match over multiple lines. Also, for matching single characters I think using square brackets is more idiomatic than using ( ... | ... )
If you want the match to be order-independent then use this:
(?:text1[\w\W]*text2)|(?:text2[\w\W]*text1)
Adding a response for IntelliJ
Building on #OnlineCop's answer, to swap the order of two expressions in IntelliJ,you would style the search as in the accepted response, but since IntelliJ doesn't allow a one-line version, you have to put the replace statement in a separate field. Also, IntelliJ uses $ to identify expressions instead of \.
For example, I tend to put my nulls at the end of my comparisons, but some people prefer it otherwise. So, to keep things consistent at work, I used this regex pattern to swap the order of my comparisons:
Notice that IntelliJ shows in a tooltip what the result of the replacement will be.
For me works text1*{0,}(text2){0,}.
With {0,} you can decide to get your keyword zero or more times OR you set {1,x} to get your keyword 1 or x-times (how often you want).
I am searching for specific terms in a block of text only when they are surrounded by other 'qualifying' terms. I have made a regex where each of those qualifying terms is an option:
(?<QUALIFY_A>(A\s(?<TERM>(Hi|Bye)))|(?<QUALIFY_B>B\s(?<TERM1>(Hi|Bye)))|(?<QUALIFY_C>C\s(?<TERM2>(Hi|Bye))))
http://regex101.com/r/xR0uA9
So far this behaves as I expect as it finds the first match of the term when preceded by the qualifying expression. However Ideally I'd like to get each one returned, in other words not have the regex quit once matched. I realize if I just had one option I could do /1/2 to get multiple matches to the one option but in this case trying to get multiple matches to different options.
try putting g in the modifier box you can see on the right hand side of the regex
I've been stuck for a while on this Sublime Snippet now.
I would like to display the correct package name when creating a new class, using TM_FILEPATH and TM_FILENAME.
When printing TM_FILEPATH variable, I get something like this:
/Users/caubry/d/[...]/src/com/[...]/folder/MyClass.as
I would like to transform this output, so I could get something like:
com.[...].folder
This includes:
Removing anything before /com/[...]/folder/MyClass.as;
Removing the TM_FILENAME, with its extension; in this example MyClass.as;
And finally finding all the slashes and replacing them by dots.
So far, this is what I've got:
${1:${TM_FILEPATH/.+(?:src\/)(.+)\.\w+/\l$1/}}
and this displays:
com/[...]/folder/MyClass
I do understand how to replace splashes with dots, such as:
${1:${TM_FILEPATH/\//./g/}}
However, I'm having difficulties to add this logic to the previous one, as well as removing the TM_FILENAME at the end of the logic.
I'm really inexperienced with Regex, thanks in advance.
:]
EDIT: [...] indicates variable number of folders.
We can do this in a single replacement with some trickery. What we'll do is, we put a few different cases into our pattern and do a different replacement for each of them. The trick to accomplish this is that the replacement string must contain no literal characters, but consist entirely of "backreferences". In that case, those groups that didn't participate in the match (because they were part of a different case) will simply be written back as an empty string and not contribute to the replacement. Let's get started.
First, we want to remove everything up until the last src/ (to mimic the behaviour of your snippet - use an ungreedy quantifier if you want to remove everything until the first src/):
^.+/src/
We just want to drop this, so there's no need to capture anything - nor to write anything back.
Now we want to match subsequent folders until the last one. We'll capture the folder name, also match the trailing /, but write back the folder name and a .. But I said no literal text in the replacement string! So the . has to come from a capture as well. Here comes the assumption into play, that your file always has an extension. We can grab the period from the file name with a lookahead. We'll also use that lookahead to make sure that there's at least one more folder ahead:
^.+/src/|\G([^/]+)/(?=[^/]+/.*([.]))
And we'll replace this with $1$2. Now if the first alternative catches, groups $1 and $2 will be empty, and the leading bit is still removed. If the second alternative catches, $1 will be the folder name, and $2 will have captured a period. Sweet. The \G is an anchor that ensures that all matches are adjacent to one another.
Finally, we'll match the last folder and everything that follows it, and only write back the folder name:
^.+/src/|\G([^/]+)/(?=[^/]+/.*([.]))|\G([^/]+)/[^/]+$
And now we'll replace this with $1$2$3 for the final solution. Demo.
A conceptually similar variant would be:
^.+/src/|\G([^/]+)/(?:(?=[^/]+/.*([.]))|[^/]+$)
replaced with $1$2. I've really only factored out the beginning of the second and third alternative. Demo.
Finally, if Sublime is using Boost's extended format string syntax, it is actually possible to get characters into the replacement conditionally (without magically conjuring them from the file extension):
^.+/src/|\G(/)?([^/]+)|\G/[^/]+$
Now we have the first alternative for everything up to src (which is to be removed), the third alternative for the last slash and file name (which is to be removed), and the middle alternative for all folders you want to keep. This time I put the slash to be replaced optionally at the beginning. With a conditional replacement we can write a . there if and only if that slash was matched:
(?1.:)$2
Unfortunately, I can't test this right now and I don't know an online tester that uses Boost's regex engine. But this should do the trick just fine.
I am trying to run some regular expressions(grep) on a text file of about 4K lines. The main portion that I need replaced looks like this:
1,"An Internet-Ready Resume",1,2,"","
And I need it to look like this:
<item>
<title>An Internet-Ready Resume</title>
<category>1</category>
<author>2</author>
<content>
So far, this is what I was trying to no avail:
[0-9]{1}\,\"*\"\,[0-9]\,[0-9]\,\"\"\,\"
You should start with doing a little reading on regular expressions. There are tons of useful resources online. Then you would see that:
you needn't escape everything (such as commas or quotes)
the asterisk * doesn't mean anything, but zero or more times
the any character is the . character. .* means any character any number of times (or anything)
if you need to make substitutions where you need atoms of what you're searching, you have to set those atoms by using (<atom content>) where <atom content> is a bit of a regexp.
A tip to start: instead of \"*\" try ".*"; Check the reference.
Also note that the part regarding the replacement will depend on the text editor/tool you're using. Usually a regexp such as (a)(b) (where a,b are regexp atoms) being replaced by x\1y\2z would produce xaybz.
The error is the \"*\" part. When you use the * operator you need to tell it what is to be repeated. As written it is going to repeat the previous quote character. Instead of that you should tell it to repeat any character (.), thus: \".*\"
A secondary comment is that you have a lot of unnecessary backslashes. In fact, none of them are necessary as far as I can tell. Without them your regex looks like:
[0-9],".*",[0-9],[0-9],"","