Milacay
Milacay

Reputation: 1497

Regular Expression - Matching first occurrence ONLY of every line

I am very new to RegExp. Please help me with matching the first occurrence of every line in the data below. I am using NotePad++.

Here is my text:

ShipAddressName [varchar](100) '../../../ShipTo/AddressName',
ShipAddressContact [varchar](100) '../../../ShipTo/AddressContact',
ShipAddressLine1 [varchar](100) '../../../ShipTo/AddressLine1',
ShipAddressLine2 [varchar](100) '../../../ShipTo/AddressLine2',
ShipCity [varchar](100) '../../../ShipTo/City',
ShipState [varchar](100) '../../../ShipTo/State',
ShipZipCode [varchar](100) '../../../ShipTo/ZipCode',

If I use This ('../) in Find What, it would work. However, I would like to learn how to match ONLY the first ../ (without the single-quote), so I can replace it with ../../

Upvotes: 0

Views: 968

Answers (3)

maraaaaaaaa
maraaaaaaaa

Reputation: 8193

If you want to do this by code:

string input = @"ShipAddressName [varchar](100) '../../../ShipTo/AddressName',
    ShipAddressContact [varchar](100) '../../../ShipTo/AddressContact',
    ShipAddressLine1 [varchar](100) '../../../ShipTo/AddressLine1',
    ShipAddressLine2 [varchar](100) '../../../ShipTo/AddressLine2',
    ShipCity [varchar](100) '../../../ShipTo/City',
    ShipState [varchar](100) '../../../ShipTo/State',
    ShipZipCode [varchar](100) '../../../ShipTo/ZipCode',";

string pattern = @"(.+?)(\.\.\/)(.+\n?)";

string replacement = @"$1$2$2$3";

Console.Log(Regex.Replace(input,
    string pattern,
    string replacement
));

// Output:
// Execution Time(sec.):
// 0.000022

// Raw Match Pattern:
// (.+?)(\.\.\/)(.+\n?)

// Raw Replace Pattern:
// $1$2$2$3

// $sourcestring after replacement:
// ShipAddressName [varchar](100) '../../../../ShipTo/AddressName',
// ShipAddressContact [varchar](100) '../../../../ShipTo/AddressContact',
// ShipAddressLine1 [varchar](100) '../../../../ShipTo/AddressLine1',
// ShipAddressLine2 [varchar](100) '../../../../ShipTo/AddressLine2',
// ShipCity [varchar](100) '../../../../ShipTo/City',
// ShipState [varchar](100) '../../../../ShipTo/State',
// ShipZipCode [varchar](100) '../../../../ShipTo/ZipCode',

Upvotes: 2

Steven Doggart
Steven Doggart

Reputation: 43743

You could just find '\.\./ and replace it with '../../, but if you really just want to match just the first occurrence without including the single-quote, you could just search for (?<=')\.\./ and replace it with ../../.

Here is the meaning of the pattern:

  • (?<=') - A positive look-behind which looks for the single-quote character. A look-behind states that only strings which are preceded by certain text are eligible as a match (but that preceding text is not considered part of a match)
    • (?<= - Begins a positive look-behind sub-pattern
    • ' - The character to look for
    • ) - Ends the look-behind sub-pattern
  • \.\./ - The text to match
    • \. - An escaped period (so that regex doesn't interpret the period as the any-character character-set

Alternatively, you could probably just search for (?<='\.\./) and replace it with ../. That would merely match the position, but not any of the text, so when it performs the replacement, it acts as a simple insertion.

On a side-note, there are also negative look-behinds, which indicate that the given text cannot precede a match. There are also positive and negative look-aheads.

Upvotes: 2

mason81
mason81

Reputation: 1750

This answer is very specific to Notepad++

To ADD an extra ../ use the following find and replace patterns (with regex setting checked):

Find: '(\.\./)

Replace: '$1$1

Example when entered properly in Notepad++

To REMOVE one ../ use the following find and replace patterns (with regex setting checked):

Find: '(\.\./)

Replace: '

Example when entered properly in Notepad++

Upvotes: 1

Related Questions