0

I want search for specific pattern like 'constant_string' : some_string. 'constant_string' : this will be constant part in pattern & some_string it may change.

I want to find for this pattern 'constant_string' : some_string and replace with 'constant_string' : 'some_string'. (single quote added to some_string)

Omi
  • 3,544
  • 5
  • 18
  • 38

1 Answers1

1

Try using:

(?<='constant_string' \: )([A-Za-z0-9_]*)

And replace with:

'\1'

Captures the string into a capturing group and uses it to replace the quoted value

Demo : https://regex101.com/r/Hl3Zpe/1

Better Method:

('constant_string' \: )([A-Za-z0-9_]*)

Replace with

'\2'

Using 2 here, since there are two capturing groups and \1 is now constant_string : and the variable string is in \2

Dhananjai Pai
  • 5,397
  • 1
  • 6
  • 23
  • can you please explain when to use '\1' & '\2' – Omi Feb 06 '19 at 10:29
  • In the first case there is only one capture group, the former part is only the lookahead In the second case I am using two capture groups, and the variable string is actually the 2nd one – Dhananjai Pai Feb 06 '19 at 10:31
  • that means () brackets decides the capture group correct? if there are ()()() so there will be 3 capturing group? please correct me if i am wrong. – Omi Feb 06 '19 at 10:35
  • More or less, @Omi , though in the first case it does not form a capturing group even with () since it is a lookahead – Dhananjai Pai Feb 06 '19 at 10:36
  • refer here : [ https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean/22944075#22944075 ] – Dhananjai Pai Feb 06 '19 at 10:38
  • I want to learn regular expression, can you please suggest me any website or tutorial if you have. – Omi Feb 06 '19 at 11:02
  • regex101 is a good playground to test and explains to you how everything works in the right side bar. They also have a mini doc on the right bottom. – Dhananjai Pai Feb 06 '19 at 11:03