정규식 : AND 연산자가 있습니까?
분명히 |
(파이프?)를 사용하여을 나타낼 수 OR
있지만 표현 하는 방법 AND
도 있습니까?
특히, 특정 구절을 모두 포함하지만 특정 순서는없는 텍스트 단락을 일치시키고 싶습니다.
비소비 정규식을 사용하십시오.
일반적인 (예 : Perl / Java) 표기법은 다음과 같습니다.
(?=
expr)
이것은 " expr 과 일치 하지만 그 후에는 원래 일치 지점에서 계속 일치 "를 의미 합니다.
원하는만큼 이러한 작업을 수행 할 수 있으며 "and"가됩니다. 예:
(?=match this expression)(?=match this too)(?=oh, and this)
일부 데이터를 저장해야하는 경우 비소비 표현식 내부에 캡처 그룹을 추가 할 수도 있습니다.
다른 응답자 중 일부가 말했듯이 미리보기를 사용해야하지만 미리보기는 대상 단어와 현재 일치 위치 사이의 다른 문자를 고려해야합니다. 예를 들면 :
(?=.*word1)(?=.*word2)(?=.*word3)
.*
첫 내다보기에 그것은 그것 "단어 1"에 도달하기 전에 필요하지만 많은 문자와 일치 할 수 있습니다. 그런 다음 일치 위치가 재설정되고 두 번째 예견은 "word2"를 찾습니다. 다시 재설정하면 마지막 부분이 "word3"과 일치합니다. 확인하는 마지막 단어이기 때문에 예견 할 필요는 없지만 아프지는 않습니다.
전체 단락을 일치 시키려면 정규식을 양쪽 끝에 고정하고 최종 .*
문자를 추가 하여 나머지 문자 를 사용해야합니다 . Perl 스타일 표기법을 사용하면 다음과 같습니다.
/^(?=.*word1)(?=.*word2)(?=.*word3).*$/m
'm'수정자는 다중 선 모드 용입니다. 그것은 할 수 있습니다 ^
및 $
단락 경계 (정규식 발언에서 "라인 경계")에 일치. 이 경우 점 메타 문자가 줄 바꿈 및 다른 모든 문자와 일치하도록하는 's'수정자를 사용 하지 않는 것이 중요 합니다.
마지막으로, 긴 단어의 일부가 아닌 전체 단어와 일치하는지 확인하기 위해 단어 경계를 추가해야합니다.
/^(?=.*\bword1\b)(?=.*\bword2\b)(?=.*\bword3\b).*$/m
이 예를보십시오.
2 개의 정규 표현식 A와 B가 있고 둘 다 일치 시키려고하므로 의사 코드에서 다음과 같이 보입니다.
pattern = "/A AND B/"
다음과 같이 AND 연산자를 사용하지 않고 작성할 수 있습니다.
pattern = "/NOT (NOT A OR NOT B)/"
PCRE :
"/(^(^A|^B))/"
regexp_match(pattern,data)
정규 표현식으로 할 수 있지만 아마도 다른 것을 원할 것입니다. 예를 들어 여러 regexp를 사용하고 if 절에서 결합하십시오.
다음과 같이 표준 정규식을 사용하여 가능한 모든 순열을 열거 할 수 있습니다 (순서에 관계없이 a, b 및 c와 일치).
(abc)|(bca)|(acb)|(bac)|(cab)|(cba)
그러나 두 개 이상의 용어가있는 경우 이것은 매우 길고 비효율적 인 정규식을 만듭니다.
Perl 또는 Java와 같은 확장 정규식 버전을 사용하는 경우 더 나은 방법이 있습니다. 다른 답변은 긍정적 인 미리보기 작업을 사용하도록 제안했습니다.
AND 연산자는 RegExp 구문에 내재 되어 있습니다.
OR 연산자는 대신 파이프로 지정해야합니다.
다음 RegExp :
var re = /ab/;
문자 의미 a
와 편지를 b
.
그룹에서도 작동합니다.
var re = /(co)(de)/;
이는 그룹 수단 co
AND 그룹 de
.
(암시 적) AND를 OR로 바꾸려면 다음 줄이 필요합니다.
var re = /a|b/;
var re = /(co)|(de)/;
Is it not possible in your case to do the AND on several matching results? in pseudocode
regexp_match(pattern1, data) && regexp_match(pattern2, data) && ...
Why not use awk?
with awk regex AND, OR matters is so simple
awk '/WORD1/ && /WORD2/ && /WORD3/' myfile
If you use Perl regular expressions, you can use positive lookahead:
For example
(?=[1-9][0-9]{2})[0-9]*[05]\b
would be numbers greater than 100 and divisible by 5
You could pipe your output to another regex. Using grep, you could do this:
grep A | grep B
In addition to the accepted answer
I will provide you with some practical examples that will get things more clear to some of You. For example lets say we have those three lines of text:
[12/Oct/2015:00:37:29 +0200] // only this + will get selected
[12/Oct/2015:00:37:x9 +0200]
[12/Oct/2015:00:37:29 +020x]
See demo here DEMO
What we want to do here is to select the + sign but only if it's after two numbers with a space and if it's before four numbers. Those are the only constraints. We would use this regular expression to achieve it:
'~(?<=\d{2} )\+(?=\d{4})~g'
Note if you separate the expression it will give you different results.
Or perhaps you want to select some text between tags... but not the tags! Then you could use:
'~(?<=<p>).*?(?=<\/p>)~g'
for this text:
<p>Hello !</p> <p>I wont select tags! Only text with in</p>
See demo here DEMO
The order is always implied in the structure of the regular expression. To accomplish what you want, you'll have to match the input string multiple times against different expressions.
What you want to do is not possible with a single regexp.
Use AND outside the regular expression. In PHP lookahead operator did not not seem to work for me, instead I used this
if( preg_match("/^.{3,}$/",$pass1) && !preg_match("/\s{1}/",$pass1))
return true;
else
return false;
The above regex will match if the password length is 3 characters or more and there are no spaces in the password.
참고URL : https://stackoverflow.com/questions/469913/regular-expressions-is-there-an-and-operator
'developer tip' 카테고리의 다른 글
CSS 배경 늘리기 및 크기 조정 (0) | 2020.10.02 |
---|---|
.BAT 파일 내에서 여러 .BAT 파일을 실행하는 방법 (0) | 2020.10.02 |
객체 리터럴 / 이니셜 라이저의 자체 참조 (0) | 2020.10.02 |
버튼을 클릭 할 때 대화 상자가 닫히지 않도록하는 방법 (0) | 2020.09.30 |
DataFrame 열의 순서를 변경하는 방법은 무엇입니까? (0) | 2020.09.30 |