Eclipse, 정규식 검색 및 바꾸기
이클립스에서 정규 표현식 검색 및 바꾸기를 수행 할 때 일치하는 검색 문자열을 바꾸기 문자열의 일부로 사용할 수 있습니까?
기본적으로 모든 발생을 대체하고 싶습니다.
variableName.someMethod()
와:
((TypeName)variableName.someMethod())
여기서 variableName 은 모든 변수 이름이 될 수 있습니다.
sed 에서는 다음과 같은 것을 사용할 수 있습니다.
s/[a-zA-Z]+\.someMethod\(\)/((TypeName)&)/g
즉, & 는 일치하는 검색 문자열을 나타냅니다. 이클립스에 비슷한 것이 있습니까?
감사!
예, "()"는 그룹을 캡처합니다. $ i와 함께 다시 사용할 수 있습니다. 여기서 i는 i 번째 캡처 그룹입니다.
그래서:
검색:
(\w+\.someMethod\(\))
바꾸다:
((TypeName)$1)
힌트 : 텍스트 상자의 CTRL+ Space는 정규 표현식 작성에 대한 모든 종류의 제안을 제공합니다.
...
검색 사용 = (^. * import) (. *) (\ (. * \) :)
바꾸기 = $ 1 $ 2
... 대체 ...
from checks import checklist(_list):
...와...
from checks import checklist
정규식의 블록은 괄호 ( "\"로 시작하지 않음)로 구분됩니다
(^. * import)
. 다음에 "("를 만나고 $ 2까지로드 할 때까지 모든 것 ". $ 2는 다음 부분 때문에"( "에서 멈 춥니 다 (아래 다음 줄 참조)
(\ (. * \) :) "$ 2 블록을 시작한 후 $ 2 블록을 중지하고 $ 3을 시작하십시오. $ 3은"( 'any text') : "또는"(_list) : "와 함께로드됩니다.
그런 다음 바꾸기에서 세 블록을 모두 처음 두 블록으로 바꾸려면 $ 1 $ 2입니다.
NomeN has answered correctly, but this answer wouldn't be of much use for beginners like me because we will have another problem to solve and we wouldn't know how to use RegEx in there. So I am adding a bit of explanation to this. The answer is
search: (\w+\.someMethod\(\))
replace: ((TypeName)$1)
Here:
In search:
First and last '(' ')' depicts a group in regex
'\w' depicts words (alphanumeric+underscore)
'+' depicts one or more(ie one or more of alphanumeric+underscore)
'.' is a special character which depicts any character( ie .+ means one or more of any character). Because this is a special character to depict a '.' we should give an escape character with it, ie '.'
'someMethod' is given as it is to be searched.
The two parenthesis '(',')' are given along with escape character because they are special character which are used to depict a group (we will discuss about group in next point)
In replace:
It is given '((TypeName)$1)', here $1 depicts the group. That is all the characters that are enclosed within the first and last parenthesis '(' ,')' in the search field
Also make sure you have checked the 'Regular expression' option in find an replace box
More information about RegEx can be found in http://regexr.com/.
At least at STS (SpringSource Tool Suite) groups are numbered starting form 0, so replace string will be
replace: ((TypeName)$0)
For someone who needs an explanation and an example of how to use a regxp in Eclipse. Here is my example illustrating the problem.
이름을 바꾸고 싶습니다
/download.mp4^lecture_id=271
에
/271.mp4
그리고이 중 여러 개가있을 수 있습니다.
수행 방법은 다음과 같습니다.
그런 다음 찾기 / 바꾸기 버튼을 누르십시오
참고 URL : https://stackoverflow.com/questions/1372748/eclipse-regular-expression-search-and-replace
'IT박스' 카테고리의 다른 글
SQL 열에서 가장 빈번한 값 찾기 (0) | 2020.07.25 |
---|---|
ObjectStateManager에서 오브젝트를 찾을 수 없으므로 오브젝트를 삭제할 수 없습니다. (0) | 2020.07.25 |
레일은 블록으로 부분적으로 렌더링 (0) | 2020.07.25 |
Java에서 천 단위 구분 기호를 설정하는 방법은 무엇입니까? (0) | 2020.07.24 |
angular.js에서 JSONP $ http.jsonp () 응답 구문 분석 (0) | 2020.07.24 |