How to ignore spaces in a string of a regular expression string, but only if it appears after a newline?

What is the best way to ignore the space in the target line when searching for matches using the regex pattern, but only if the space appears after a new line (\ n)? For example, if my search is for “cats,” I would like “c \ n ats” or “ca \ n ts” to match, but not “c ats,” since spaces do not appear after a new line. I cannot exclude spaces in advance because I need to find the start and end index of a match (including any spaces) to highlight this match, and any spaces should be there for formatting purposes.

+3
source share
3 answers

"What is the best way to ignore the space in the target line when searching for matches using the regex pattern"

I would build regex dynamically by inserting (?:\n\s)?between each character.

use strict;
use warnings;

my $needed = 'cats';
my $regex = join '(?:\n\s)?' , split ( '',$needed );

print "\nRegex = $regex\n", '-'x40, "\n\n";

my $target = "
   cats
   c ats
   c\n ats
   ca ts
   ca\n ts
   cat s
   cat\n s
";

while ( $target =~ /($regex)/g)
{
    print "Found -  '$1'\n\n";
}

Output:

Regex = c(?:\n\s)?a(?:\n\s)?t(?:\n\s)?s
----------------------------------------

Found -  'cats'

Found -  'c
 ats'

Found -  'ca
 ts'

Found -  'cat
 s'
+2
source

If your regular expression engine supports search statements, use the positive lookbehind statement to check for the previous line in a new line:

(?<=\n)\s
+3
source

I made a small ruby ​​snippet based on the rules you specified. Is this what you are looking for?

data = <<DATA
test1c\n atsOKexpected

test2ca\n tsOKexpected

test3catsOKexpected

test5ca tsBADexpected

test6 catsOKexpected

test7cats OKexpected
DATA

tests = data.split(/\n\n/)

regex = /c(\n )?a(\n )?t(\n )?s/

tests.each do |s|
  if s =~ regex
    puts "OK\n#{s}\n\n"
  else
    puts "BAD\n#{s}\n\n"
  end
end

# RESULTS
# OK
# test1c
#  atsOKexpected
# 
# OK
# test2ca
#  tsOKexpected
# 
# OK
# test3catsOKexpected
# 
# BAD
# test5ca tsBADexpected
# 
# OK
# test6 catsOKexpected
# 
# OK
# test7cats OKexpected
0
source

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


All Articles