H.C
H.C

Reputation: 608

Regex replace character (@) but only at end of phrase

https://regex101.com/

want with regex detect all phrases that end with @ so in sublime text replace with empty space for example

DECLARE @showBrand@ VARCHAR(MAX) = '1';
DECLARE @showBrandImage@ VARCHAR(MAX) = '[]';
DECLARE @ShowBrandLink@ VARCHAR(MAX) = '[]';
DECLARE @ShowbreakLineAfterSize@ VARCHAR(MAX) = '[]';
DECLARE @showbreaklineAfterStock@ VARCHAR(MAX) = '[]';
DECLARE @showBuyBtn@ VARCHAR(MAX) = '1';

and get

DECLARE @showBrand VARCHAR(MAX) = '1';
DECLARE @showBrandImage VARCHAR(MAX) = '[]';
DECLARE @ShowBrandLink VARCHAR(MAX) = '[]';
DECLARE @ShowbreakLineAfterSize VARCHAR(MAX) = '[]';
DECLARE @showbreaklineAfterStock VARCHAR(MAX) = '[]';
DECLARE @showBuyBtn VARCHAR(MAX) = '1';

so as ($ - Matches the end of a string) was trying @$ or [@]$ but doesn't detect any with [@$] detects all as it would be @ only so... what is the correct way?

Upvotes: 0

Views: 64

Answers (3)

briosheje
briosheje

Reputation: 7446

Javascript implementation:

const needle = `
DECLARE @showBrand@ VARCHAR(MAX) = '1';
DECLARE @showBrandImage@ VARCHAR(MAX) = '[]';
DECLARE @ShowBrandLink@ VARCHAR(MAX) = '[]';
DECLARE @ShowbreakLineAfterSize@ VARCHAR(MAX) = '[]';
DECLARE @showbreaklineAfterStock@ VARCHAR(MAX) = '[]';
DECLARE @showBuyBtn@ VARCHAR(MAX) = '1';
`;
console.log(needle.replace(/(@\w+)@/gm, '$1'));

I would go with (@\w+)@ with global and multiline (/gm) and replace using Group 1 match.

https://regex101.com/r/Sv1zT7/1/

The result will be

DECLARE @showBrand VARCHAR(MAX) = '1';
DECLARE @showBrandImage VARCHAR(MAX) = '[]';
DECLARE @ShowBrandLink VARCHAR(MAX) = '[]';
DECLARE @ShowbreakLineAfterSize VARCHAR(MAX) = '[]';
DECLARE @showbreaklineAfterStock VARCHAR(MAX) = '[]';
DECLARE @showBuyBtn VARCHAR(MAX) = '1';

Upvotes: 0

Emma
Emma

Reputation: 27743

You can simply use the space after the second @ as a boundary and write an expression, maybe similar to:

(.*?)(@)(\s.*)

Demo

RegEx

If this wasn't your desired expression, you can modify/change your expressions in regex101.com.

enter image description here

RegEx Circuit

You can also visualize your expressions in jex.im:

enter image description here

Upvotes: 0

nicola_castellani
nicola_castellani

Reputation: 72

you can use the following: /[@][\W]/

This regexp will check the @ character literally, and after that a single non-word character (a space for example).

Just replace what you find with a single space.

You can take a look to this regexp here : https://regex101.com/r/eeuw30/1

Upvotes: 3

Related Questions