0

I am using pyGithib library view the contents of all the files ending with *.rb in github repo, Using that library i get a string which is in this format

desc  'heading \'Test this too\')
                 Rationale: Best Practice
                 this line is also included in description '

config 'xxx' do

          title 'this is a dummy title \'Test this too\' for this block'
          desc  'Demo (test this) description \'Test this too\')
                 Rationale: Best Practice
                 this line is also included in description '
          
          tag benchmark: 'xyz:11'
          tag level: 1
          tag version: '0.0.1'
          tag reference: 'version 2.4'
          tag resource_type: 'A'

Using regex, how can i get values which are in multiline after desc and also for string containing version after the config block

krisdigitx
  • 7,068
  • 20
  • 61
  • 97

1 Answers1

1

You can do this using a matcher as shown in this question: How to extract a substring using regex

Where you would have to replace the regex with the correct regex.

To get the regex you can use sites such as https://regexr.com/ that let you easily test what matches your regex.

In this case something like this should work:

String mydata = """config 'xxx' do

      title 'this is a dummy title \'Test this too\' for this block'
      desc  'Demo (test this) description \'Test this too\')
             Rationale: Best Practice
             this line is also included in description '
      
      tag benchmark: 'xyz:11'
      tag level: 1
      tag version: '0.0.1'
      tag reference: 'version 2.4'
      tag resource_type: 'A'""";
Pattern desc_pattern = Pattern.compile("desc  '(.|\n)*'\n\s*\n");
Matcher desc_matcher = desc_pattern.matcher(mydata);

desc = desc_matcher.find()

Pattern version_pattern = Pattern.compile("tag version: '.*'");
Matcher version_matcher = version_pattern.matcher(mydata);

version = version_matcher.find()

And then you can trim off the first few characters to get the string you want.

Jkatzy
  • 81
  • 5