Simeon Mark Fernandes
Simeon Mark Fernandes

Reputation: 107

Regex to ignore whitespace

Consider 2 of the following:

  1. trans( 'Hello' )
  2. trans('Hello')

My regex: trans\(([\'"])(((?!\1).)*)\1\)

The above works well with the 2. case but does not work with case 1. I tried adding another capturing group (/s) but then 2. does not work while 1. does. Is there a way to have both detected?

Upvotes: 0

Views: 1117

Answers (3)

It looks like you want symmetrical quotes and whitespace, and to capture the string in between.

trans\((\s*)(['"])(((?!\2).)*)\2\1\)

The string is in $3.

Upvotes: 0

KaspianR
KaspianR

Reputation: 188

The following should work as it's simply modifying your expression to support any number of whitespaces on either side of the quotes:

trans\(\s*([\'"])(((?!\1).)*)\1\s*\)

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522817

The following pattern seems to work here:

trans\((\s*)(['"])(.*?)\2\1\)

Demo

Here we capture optional whitespace before the singly quoted term, and then ensure that we also match the same as \1 after that term. For those inputs having whitespace, we assert that we see the same whitespace afterwards. For inputs not having whitespace, then \1 should be empty.

Upvotes: 1

Related Questions