Using (? <! Regex assert without fixed width

I have this regex that works almost exactly as expected ...

(?<!color: )(?<!color:)(?<!pid=[0-9][0-9][0-9][0-9][0-9])\#(\w+) 

Let's say this is my html code:

 <span style='color: #FFAABB'><a href='?pid=55155#pid55155'>hey #hello</a></span> 

The regular expression only matches: #hello, which is good, but the fact is that I don’t know how many numbers will be after the "pid", and I can’t use "?", "*" Or "{n, m}" with "(? <!)" (I don’t know why).

My question is: is there a way to make it dynamic?

Please do not offer:

 (?<!color: )(?<!color:)(?<!pid=[0-9])(?<!pid=[0-9][0-9])(?<!pid=[0-9][0-9][0-9])(?<!pid=[0-9][0-9][0-9][0-9])\#(\w+) 

Because it's terrible.

Here is a working example:

https://www.regex101.com/r/rC2mH4/1

Thanks in advance:)

+5
source share
2 answers

If your language supports (*SKIP)(*F) , you can simply use below.

 (?:color:\s*|pid=\d*)#(*SKIP)(*F)|#(\w+) 

Demo

Note that the above \s also matches newlines. Therefore, use \h to match only horizontal spaces.

Explanation:

  • (?:color:\s*|pid=\d*)# Matches all characters # plus the previous color: and zero or more spaces OR | pid= and zero or more digits. So the part you don't want was mapped.

  • (*SKIP)(*F) causes the previous match to fail. And pattern after | will try to match characters from the remaining string.

  • In our case, the pattern after | equal to # . Thus, #(\w+) matches all the hash tags you want.

+2
source
 color:\s*#\w+|pid=\d+#\w+|(#\w+) 

You can try this. Just grab a grab or group. Watch the demo. It fits all the crap and captures what you want.

https://www.regex101.com/r/rC2mH4/3

 $re = "/color:\\s*#\\w+|pid=\\d+#\\w+|(#\\w+)/m"; $str = "<span style=\"font-weight: bold;\">test1<span style=\"color: #FFA500;\">test2</span>test3</span>#hello#how#are#you\n<span style=\"font-weight: bold;\">test1<span style=\"color: #FFA500;\">test2</span>test3</span>#lalala #hello\n<div class=\"post_body\" id=\"pid_58705\">\n<blockquote><cite><span> (Hoy 02:42)</span>Moroha escribió: <a class=\"quick_jump\" href=\"http://test.com/Thread-hello?pid=58672#pid58672\" rel=\"nofollow\">&nbsp;</a></cite>testing</blockquote></div>\npid=97589735935795358672#foobar\n<span style='color: #FFAABB'><a href='?pid=55155#pid55155'>hey #hello</a></span>"; preg_match_all($re, $str, $matches); 
+1
source

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


All Articles