Home > Enterprise >  Regular expression matching and remove spaces
Regular expression matching and remove spaces

Time:10-05

Please how can I get the address using regex:

Address             123 Mayor Street, LAG Branch ABC

used (?<=Address(\s))(.*(?=\s)) but it includes the spaces after "Address". Trying to get an expression that extracts the address without the spaces. (There are a couple of spaces after "Address" before "123")

Thanks!

CodePudding user response:

The pattern (?<=Address(\s))(.*(?=\s)) that you tried asserts Address followed by a single whitespace char to the left, and then matches the rest of the line asserting a whitespace char to the right.

For the example data, that will match right before the last whitespace char in the string, and the match will also contain all the whitespace chars that are present right after Address


One option to match the bold parts in the question is to use a capture group.

\bAddress\s ([^,] ,\s*\S )

The pattern matches:

  • \bAddress\s Match Address followed by 1 whitespace chars
  • ( Capture group 1
    • [^,] , Match 1 occurrences of any char except , and then match ,
  • \s*\S Match optional whitespace chars followed by 1 non whitespace chars
  • ) Close group 1

.NET regex demo (Click on the Table tab to see the value for group 1)

Note that \s and [^,] can also match a newline

A variant with a positive lookbehind to get a match only:

(?<=\bAddress\s )[^,\s][^,] ,\s*\S 

.NET Regex demo

  • Related