import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(export )?const (.* \\= \\()(.*)(\\).*\\()(?'fileContent'(\\n.*)+)\\3\\.(?'varName'[^\\}]*)\\}(?'restOfPropLine'.*$)(?'restOfFileContent'(\\n.*)+>\\n\\);)";
final String string = "export const AffiliateAccountBIFields = (props) => (\n"
+ " <SimpleShowLayout {...props} variant=\"one\">\n"
+ " <UrlField source=\"bi_url\" options={{ target: '_blank' }} />\n"
+ " <ProtectedBIConfigField source=\"bi_settings\" />\n"
+ " </SimpleShowLayout>\n"
+ ");\n\n"
+ "export const AffiliateAccountGeneralFields = (props) => (\n"
+ " <SimpleShowLayout {...props}>\n"
+ " <TextField source=\"name\" />\n"
+ " <ReferenceField\n"
+ " linkType=\"show\"\n"
+ " label=\"Platform Provider\"\n"
+ " addLabel\n"
+ " source=\"platform_provider_id\"\n"
+ " reference=\"accounts/platform-providers\"\n"
+ " allowEmpty\n"
+ " >\n"
+ " <TextField addLabel source=\"name\" />\n"
+ " </ReferenceField>\n"
+ " <ReferenceField\n"
+ " label=\"Company\"\n"
+ " addLabel\n"
+ " source=\"company_id\"\n"
+ " reference=\"companies\"\n"
+ " linkType=\"show\"\n"
+ " >\n"
+ " <TextField addLabel source=\"name\" />\n"
+ " </ReferenceField>\n"
+ " <AccountManagerField\n"
+ " label=\"Account Manager\"\n"
+ " source=\"account_manager_id\"\n"
+ " addLabel\n"
+ " />\n"
+ " <PortfolioField label=\"Portfolio\" source=\"portfolio_id\" addLabel />\n"
+ " <FunctionField\n"
+ " label=\"Status\"\n"
+ " addLabel\n"
+ " source=\"activity_status\"\n"
+ " render={(record) => {\n"
+ " if (!record.activity_status) return null;\n"
+ " const humanizedStatus = get(\n"
+ " ACTIVITY_STATUS_OPTIONS,\n"
+ " `${record.activity_status}.name`\n"
+ " );\n"
+ " if (!humanizedStatus) {\n"
+ " // eslint-disable-next-line no-console\n"
+ " console.warn(`Cannot obtain humanized value for activity_status: ${record.activity_status}`);\n"
+ " }\n"
+ " return (\n"
+ " <TextField\n"
+ " source=\"activity_status\"\n"
+ " record={{\n"
+ " ...record,\n"
+ " activity_status: humanizedStatus || record.activity_status,\n"
+ " }}\n"
+ " />\n"
+ " );\n"
+ " }}\n"
+ " />\n"
+ " <UrlField source=\"affiliate_system_link\" options={{ target: '_blank' }} />\n"
+ " <CopyToClipboardField source=\"username\">\n"
+ " <TextField source=\"username\" />\n"
+ " </CopyToClipboardField>\n"
+ " <ProtectedField source=\"password\" />\n"
+ " <CopyToClipboardField source=\"email\">\n"
+ " <TextField source=\"email\" />\n"
+ " </CopyToClipboardField>\n"
+ " <TextField source=\"security_question\" />\n"
+ " <ProtectedField source=\"security_answer\" />\n"
+ " <TextField source=\"type\" />\n"
+ " <BooleanField source=\"tracking_enabled\" />\n"
+ " <TextField source=\"tracking_variable_name\" />\n"
+ " <TextField source=\"external_affiliate_id\" />\n"
+ " <TextField source=\"revenue_posting_code\" />\n"
+ " <BooleanField source=\"loss_carry_forward\" />\n"
+ " <DateField source=\"updated_at\" locales=\"en-GB\" showTime />\n"
+ " <DateField source=\"created_at\" locales=\"en-GB\" showTime />\n"
+ " </SimpleShowLayout>\n"
+ ");\n\n"
+ "export const AffiliateAccountTabs = (props) => {\n"
+ " if (!props.record) {\n"
+ " return null;\n"
+ " }\n\n"
+ " return (\n"
+ " <TabbedShowLayout>\n"
+ " <AffiliateAccountGeneralFields {...props} label=\"General\" />\n"
+ " <AffiliateAccountBrands {...props} label=\"Brands\" />\n"
+ " <AffiliateAccountDeals {...props} label=\"Deals\" />\n"
+ " {props.hasBIPermission && (\n"
+ " <AffiliateAccountBIFields {...props} label=\"BI Settings\" />\n"
+ " )}\n"
+ " </TabbedShowLayout>\n"
+ " );\n"
+ "};\n"
+ "AffiliateAccountTabs.propTypes = {\n"
+ " record: PropTypes.object,\n"
+ " hasBIPermission: PropTypes.bool.isRequired,\n"
+ "};\n\n"
+ "export const Show = (props) => (\n"
+ " <AutoLoginStatusDialog messages={messages}>\n"
+ " <AORShow\n"
+ " {...props}\n"
+ " title={(\n"
+ " <Title\n"
+ " resource=\"Affiliate Account\"\n"
+ " type=\"show\"\n"
+ " buttons={[\n"
+ " <FavoriteButton\n"
+ " record={{ _id: props.match.params.id }}\n"
+ " resource={props.resource}\n"
+ " style={{ height: '25px' }}\n"
+ " />,\n"
+ " ]}\n"
+ " />\n"
+ " )}\n"
+ " actions={(\n"
+ " <ActionsPanel\n"
+ " customButtons={[\n"
+ " <AutoLoginButton\n"
+ " record={{ _id: props.match.params.id }}\n"
+ " resource={props.resource}\n"
+ " resourceConfig={autoLoginConfig}\n"
+ " label=\"Auto Login\"\n"
+ " />,\n"
+ " ]}\n"
+ " />\n"
+ " )}\n"
+ " >\n"
+ " <AffiliateAccountTabs hasBIPermission={props.hasBIPermission} />\n"
+ " </AORShow>\n"
+ " <NotesView isShow match={props.match} resource={props.resource} />\n"
+ " </AutoLoginStatusDialog>\n"
+ ");\n"
+ "Show.propTypes = {\n"
+ " match: PropTypes.shape({\n"
+ " params: PropTypes.shape({\n"
+ " id: PropTypes.string,\n"
+ " }),\n"
+ " }),\n"
+ " resource: PropTypes.string,\n"
+ " hasBIPermission: PropTypes.bool.isRequired,\n"
+ "};\n\n"
+ "export default connect((state) => ({\n"
+ " hasBIPermission: checkPermission(state.auth.permissions, '*', 'bi_user'),\n"
+ "}))(Show);\n";
final String subst = "$1const $2{ ${varName}, ...$3 }$4${fileContent}${varName}}${restOfPropLine}${restOfFileContent}";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html