How to get reverse regular expression?

Let's say I have a regular expression that works correctly to find all the URLs in a text file:

(http://)([a-zA-Z0-9\/\.])*

If what I want is not the URLs, but the return — all the other texts except the URLs — is there an easy modification to do this?

+2
source share
4 answers

If for some reason you only need a regular expression, try the following:

((?<=http://[a-zA-Z0-9\/\.#?/%]+(?=[^a-zA-Z0-9\/\.#?/%]))|\A(?!http://[a-zA-Z0-9\/\.#?/%])).+?((?=http://[a-zA-Z0-9\/\.#?/%])|\Z)

I’ve expanded the URL character set ( [a-zA-Z0-9\/\.#?/%]) a bit to include a few important ones, but this should by no means be precise or comprehensive.

The regular expression is a little monster, so I will try to break it:

(?<=http://[a-zA-Z0-9\/\.#?/%]+(?=[^a-zA-Z0-9\/\.#?/%])

URL-. http://[a-zA-Z0-9\/\.#?/%]+ URL-, (?=[^a-zA-Z0-9\/\.#?/%]) , URL- URL-, . , URL- , . lookbehind (?<=...), , .

URL- . \A(?!http://[a-zA-Z0-9\/\.#?/%]) (\A), , , URL- . ( URL- , , URL-, .)

OR 'd |. .+? , .

((?=http://[a-zA-Z0-9\/\.#?/%])|\Z). URL-, (?=http://[a-zA-Z0-9\/\.#?/%]). , , , \Z. , OR .

| , , OR.

( \A \Z ). , - ( perl), Start Lookahead Lookbehind.

, !

+4

, , , . Perl s/(http:\/\/)([a-zA-Z0-9\/\.])*//g

, , .

+7

If I understand the question correctly, you can use search / replace ... just a wildcard around your expression, and then replace the first and last parts.

s/^(.*)(your regex here)(.*)$/$1$3/
+1
source

im not sure if this will work exactly as you plan, but it may help: Everything that you put in brackets [] will be matched. If you put ^ inside the brackets, that is, [^ a-zA-Z0-9 /.], It will match everyone except what is in the brackets.

http://www.regular-expressions.info/

0
source

Source: https://habr.com/ru/post/1712868/


All Articles