1

I want to match string1 and anything that appears in the following lines:

['string1','string2','string3']
['string1' , 'string2' , 'string3']
['string1.domain.com' , 'string2.domain.com' , 'string3.domain.com']
['string1.domain.com:8080' , 'string2.domain.com:8080' , 'string3.domain.com:8080']

Until it encounters the following:

string2

So with the right regex in the above 4 cases the results in bold would be matched:

['string1','string2','string3']

['string1' , 'string2' , 'string3']

['string1.domain.com' , 'string2.domain.com' , 'string3.domain.com']

['string1.domain.com:8080' , 'string2.domain.com:8080' , 'string3.domain.com:8080']


I tried using the following thread to solve my issue with https://regex101.com/

The regex I tried is from Question 8020848, but was not successful with matching the string correctly:

((^|\.lpdomain\.com:8080' , ')(string1))+$

But I was not successful in only matching the part I wanted to in this text:

['string1.domain.com:8080' , 'string2.domain.com:8080' , 'string3.domain.com:8080']

The following is what I received using the regex that you suggested

@@ -108,7 +108,7 @@ node stringA, stringB, stringC,stringD inherits default {
   'ssl_certificate_file' => 'test.domain.net_sha2_n.crt',
   'ssl_certificate_key_file'=> 'test.domain.net_sha2.key' }
 },
-    service_upstream_members         => ['string1.domain.com:8080', 'string2.domain.com:8080', 'string3.domain.com:8080', 'string4.domain.com:8080', 'string5.domain.com:8080'],
+    service_upstream_members         => [ 'string2.domain.com:8080', 'string3.domain.com:8080', 'string4.domain.com:8080', 'string5.domain.com:8080'],
 service2_upstream_members      => ['string9:8080','string10:8080'],
 service3_upstream_members  => ['string11.domain.com:8080','string12.domain.com:8080','string13.domain.com:8080'],
 service_name                      => 'test_web_nginx_z1',

As you can see, there is a preceding space that for some reason wasn't removed, even regex101.com demonstrates that all whitespaces are captured in the regex using

'string1[^']*'\s*,\s*

This is what I'm currently using (where server is a variable already defined in the script)

sed -i '' "s/'${server}[^']*'\s*,\s*//"
11
  • What about a 'string1[^']*'? Commented Nov 6, 2016 at 11:09
  • Good, but won't include spaces. There are several combinations that could be: ','string2 OR ' , 'string2 OR ', 'string2 need it to include everything up until the ' immediately before string2 Commented Nov 6, 2016 at 11:11
  • 1
    You mean you need spaces + comma + spaces, too? 'string1[^']*'\s*,\s*? Commented Nov 6, 2016 at 11:12
  • 1
    Try replacing both \s with [[:space:]]. Not sure it will help, I am no expert in Mac OSX, but yes, there is a difference as far as I know. Commented Nov 9, 2016 at 8:12
  • 1
    That did the job. Commented Nov 9, 2016 at 8:16

2 Answers 2

1

To match a string starting with ' then having string1, then any chars other than ', 0 or more occurrences, and then optional number of whitespaces, a comma and again 0+ whitespaces, you may use

'string1[^']*'\s*,\s*

See the regex demo.

Breakdown:

  • 'string1 - a literal char sequence 'string1
  • [^']* - zero or more (*) characters other than ' (due to the negated character class [^...])
  • ' - an apostrophe
  • \s* - 0+ whitespaces
  • , - a comma
  • \s* - 0+ whitespaces.
Sign up to request clarification or add additional context in comments.

Comments

1

This should match what you ask (according to your bold highlights) allowing for an unknown amount of spaces, etc.

(?:…) is a non-capturing group.
…+? is a non-greedy match (as few as possible of x)

(string1.+?)(?:'string2)

(string1.+?)'string2

See example: https://regex101.com/r/lFPSEM/3

6 Comments

The (?:'string2) should not be inside a non-capturing group, there is only 1 branch inside the grouping construct here, and it is redundant.
You are right if you assume a consistent sequence of spaces, commas and single quotes is used. But your example would break as soon as that sequence is missing. I believe my approach reflects the OP's thinking of "from this until that" more closely.
If it is that, I'd close the question as a dupe of stackoverflow.com/questions/12736074/…, and the right pattern is ('string1.+?)'string2
Yup. A redundant non-capturing group :)
The (string1.+?)'string2 will capture "string1', 'string0.sssss', " in ['string1', 'string0.sssss', 'string2','string3'] and I doubt it is what is required judging by the provided input.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.