regex - Regular Expression to match word in the middle but at the end? (php is crashing :( -


i want find lines between label , return no line indent. example:

mylabel: bla if(no)   return else   foo return 

if use last return other word. e.g. send works.

$r1 = '^(\w[\w\d_]*:\s*\n((?!\nreturn).)*)(\n[^\s][^n]*\n)((((?!\nreturn).)*)\nsend)'; ; working regex 

but $r2 doesn't work. perl crashes.

$r2 = '^(\w[\w\d_]*:\s*\n((?!\nreturn).)*)(\n[^\s][^n]*\n)((((?!\nreturn).)*)\nreturn)'; ; dont working regex 

here example in php testing

$str = '^(\w[\w\d_]*:\s*\n((?!\nreturn).)*)(\n[^\s][^n]*\n)((((?!\nreturn).)*)\nreturn)'; $actual = preg_replace('/^'.$str.'/smi', "$1" . $indentstr . "$2$3", $actual); 

if not work use loop throw source code line. use prettyfy autohotkey source code tool: https://github.com/sl5net/sl5_ahk_refactor_engine

your pattern complicated , uses "famous" trick: ((?!\nreturn).)* slow , doesn't prevent lot of backtracking if subpatterns after fail.

you can write pattern in more simple way:

$pattern = '~^\w+:\r(?:\n*\r)*?return$~m'; 

demo

details:

~            # pattern delimiter ^            # anchor start of line (m option) \w+:         # label name \r           # alias kind of newline sequences (?:\n*\r)*?  # lines until (non-greedy number of line) return       # "return" $            # end of line (remove if uneeded) ~m           # pattern delimiter, multiline option 

\n matches character except newline whatever mode (singleline or not). in case can replace dot, less explicit.

\r alias several sequences of newlines \r\n, \n or more exotic. if know kind of newline sequence used in string, replace sequence.

see other version

in short, pattern designed test if line begins "return" @ start of line (not @ positions in string).


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -