I need to find a match over the following text and I need to detect only one single under score by using REGEX:
How can I accomplish this requirement in APEX?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.Sign up to join this community
This searches for _ that is not followed by _c
Try it here: https://regex101.com/
You can test it this way:
String test1 = 'Opportunity__c'; String test2 = 'Opportunity_abc__c'; String regex = '_[^_c]'; System.debug(test1.split(regex).size() != 1); // false System.debug(test2.split(regex).size() != 1); // true
This may be a bit wierd, but if string does not contain regex, it will not be split (1 element as result) and if it contains, it will have at least 2 elements.
If you do not need regex you could simply do:
If you are in Apex Not sure that regex would be required but that is up to you.
The regex (match all single non repeated occurrences of the character) would be:
(?<!_)_(?!_) and will need to be adjusted for Apex.