Cropping string using reqex match

I need to use a crippled tool that provides no way to trim leading spaces from a string. It has a .NET-style regular expression, but only Match is implemented, not replaced. So, I have come (surprise yourself) with this regular expression, which seems to work .. but I do not quite understand why it works : -)

$trimmed = regex/[^ ].*[^ ]/ ($original_string)

Why does this work, does it really work in all cases, and is there a better way if you only have a regular match (even group matches cannot be captured :()?

+3
source share
2 answers

It should work fine if only one character is not surrounded by a space.

:

  • [^ ]
  • , ( ) .*
  • [^ ]

, (1 3), .

\b [^ ], " ", :

\b.*\b
+3

: [^ ] , .* -, [^ ] . , , $original_string.

, , . , (. \b ).

+2

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


All Articles