import Foundation
let pattern = #"""
(?<=regex101.*)\.NET
class MyNewLanguage extends PureComponent {
//
// Proptypes
static propTypes = {
regex: PropTypes.string.isRequired,
flags: PropTypes.string.isRequired,
delimiter: PropTypes.string.isRequired,
testString: PropTypes.string.isRequired,
isSubstituting: PropTypes.bool.isRequired,
isGlobal: PropTypes.bool.isRequired,
substString: PropTypes.string
};
//
// Control
// If you need to manipulate any data, you should preferably do it in a function defined
// on the class body here. This could for example be sanitizing data (escaping quotes, etc).
//
// Render functions
render() {
return (
<Highlight lang="myNewLanguage">
{this._renderCode()}
</Highlight>
);
}
_renderCode() {
const { regex, flags, delimiter, testString, isSubstituting, substString, isGlobal } = this.props;
const codeString = new CodeString();
// CodeString is a basic class that allows you to create a code snippet without having
// to worry about indentation or newlines.
// The only functions are `append`, `indent` and `toString`.
// The implementation can be found on this page.
// Create your code string here
codeString.append(`Use string literals when ${regex} interpolating ${flags} data`);
return codeString.toString();
}
}
export default MyNewLangauge;
"""#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"regex101 now has 100% .NET support"#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let matches = regex.matches(in: testString, range: stringRange)
var result: [[String]] = []
for match in matches {
var groups: [String] = []
for rangeIndex in 1 ..< match.numberOfRanges {
let nsRange = match.range(at: rangeIndex)
guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue }
let string = (testString as NSString).substring(with: nsRange)
groups.append(string)
}
if !groups.isEmpty {
result.append(groups)
}
}
print(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 Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression