1

I am looking for a regular expression for locating numerous expressions to find and replace. The expression looks like s360a__fieldname__c. I need to find all the instances where the s360a__ is then followed by the __c.

The issue is that it has to be within the one line so it is not finding a starting s360a__ and then the next __c which may be several lines below.

Here is an example of some of the xml I am changing.

<fields>
    <fullName>s360a__AddressPreferredStreetAddressCity__c</fullName>
    <deprecated>false</deprecated>
    <externalId>false</externalId>
    <label>Preferred Street City</label>
    <length>255</length>
    <required>false</required>
    <trackFeedHistory>false</trackFeedHistory>
    <trackHistory>false</trackHistory>
    <type>Text</type>
    <unique>false</unique>
</fields>
<fields>
    <fullName>s360a__AddressPreferredStreetAddressCountry__c</fullName>
    <deprecated>false</deprecated>
    <externalId>false</externalId>
    <label>Preferred Street Country</label>
    <picklist>

1 Answers1

0

You'd better of using a parser, combined with an xpath instead. Here's an example with PHP (can easily be adopted for e.g. Python as well). The idea is to load the DOM, then use a function to filter out elements (starts-with() and text() in this example):

<?php
$xml = '<fields>
    <fullName>s360a__AddressPreferredStreetAddressCity__c</fullName>
    <deprecated>false</deprecated>
    <externalId>false</externalId>
    <label>Preferred Street City</label>
    <length>255</length>
    <required>false</required>
    <trackFeedHistory>false</trackFeedHistory>
    <trackHistory>false</trackHistory>
    <type>Text</type>
    <unique>false</unique>
</fields>';

$dom = simplexml_load_string($xml);
// find everything where the text starts with 's360a_'
$fields = $dom->xpath("//*[starts-with(text(), 's360a_')]");
print_r($fields);
# s360a__AddressPreferredStreetAddressCity__c

The code checks if the text starts with s360a_. To actually check if it also ends with some specific string, you need to fiddle quite a bit (as the corresponding function ends-with() is not yet supported).

# check if the node text starts and ends with a specific string     
$fields = $dom->xpath("//*[starts-with(., 's360a_') and substring(text(), string-length(text()) - string-length('_c') +1) = '_c']");
?>
Community
  • 1
  • 1
Jan
  • 42,290
  • 8
  • 54
  • 79