Regex replaces all spaces after group on line

Ok ... this question sounds strange, but I mean the following:

I have a specific group to search for, and I want every space AFTER that group, but only on the current line, to be removed. My specific example:

@subpackage Some Word Stuff

      

@subpackage

does not accept spaces, but I didn’t know that at the time, and I have a lot of many of these lines to fix. I would like to find and replace a regex (my IDE supports this) to strip the spaces between words after EACH instance @subpackage

.

EDIT: Clarity by example, maybe

"@subpackage Some Word Stuff" -> "@subpackage SomeWordStuff"

      

+3


source to share


1 answer


Find with the following regular expression replace with an empty string ''

.
(Just use replace_all)

 # '~(?mi-)(?:(?!\A)\G|^@subpackage)[^ \r\n]*\K[ ]+~'

 (?xmi-)                     # Inline 'Expanded, multiline, case insensitive' modifiers
 (?:
      (?! \A )                    # Matched before, start from here
      \G                          
   |                            # or,
      ^ @subpackage               # '@Subpackage' at bol (remove '^' if not at bol)
 )
 [^ \r\n]*                   # Not space or line breaks
 \K                          # Don't include anything from here back in match
 [ ]+                        # 1 or more spaces

      



Here is one of all spaces without lines.

 # '~(?mi-)(?:(?!\A)\G|^@subpackage)\S*\K[^\S\r\n]+~'

 (?xmi-)                     # Inline 'Expanded, multiline, case insensitive' modifiers
 (?:
      (?! \A )                    # Matched before, start from here
      \G                          
   |                            # or,
      ^ @subpackage               # '@Subpackage' at bol (remove '^' if not at bol)
 )
 \S*                         # 0 or more, Not whitespace
 \K                          # Don't include anything from here back in match
 [^\S\r\n]+                  # 1 or more non-linebreak whitespaces

      

+1


source







All Articles