I need to find a match over the following text and I need to detect only one single under score by using REGEX:
Text: network_ip__c
Result: true
Text: Opportunity__c
Result: false
How can I accomplish this requirement in APEX?
I need to find a match over the following text and I need to detect only one single under score by using REGEX:
Text: network_ip__c
Result: true
Text: Opportunity__c
Result: false
How can I accomplish this requirement in APEX?
Try this
_[^_c]
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:
[string].replace('__','').contains('_');
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.