如何使用正则表达式在语句周围加上引号 [英] How can I use regex to put quotes around a statement

查看:71
本文介绍了如何使用正则表达式在语句周围加上引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 regexkitlite 在 xcode 中验证我的 iPhone 应用程序中的一些数据.

I am using regexkitlite to validate some data in my iPhone app in xcode.

我正在进行一个 api 调用,它会发送一个 json 结果:

I am making an api call that sends a json result of:

"taskDate": newDate("September 23, 2011 00:00:00")

我如何使用正则表达式将其转换为:

how do i use regex to convert it to:

"taskDate": "newDate("September 23, 2011 00:00:00")"

我想用引号将每个taskdate"键的值括起来.

I want to surround the value of every "taskdate" key with quotes.

添加 OP 的评论

这是我正在使用的:

[resultString replaceOccurrencesOfRegex:@"new Date((.*?)\")," withString:@"\"\"," range:NSMakeRange(0, [resultString length])];

其中 resultString 是包含new Date(...."的字符串

where resultString is the string containing the "new Date(...."

推荐答案

您可以使用正则表达式来识别文本中taskDate"格式的文本位置:newDate("September 23, 2011 00:00:00")" 但实际的替换你必须自己写.正则表达式不会替换字符串,它会在字符串中查找模式.现在,为了找到 "taskDate" 的模式: newDate("") 你可以使用

You can use regex to identify where in your text you have text in the format of "taskDate": "newDate("September 23, 2011 00:00:00")" but the actual replacement you will have to write yourself. Regex doesn't replace strings, it finds patterns within a string. Now, in order to find the pattern of "taskDate": newDate("<anything can go here>") you can use

"taskDate"\: newDate\(".*?"\)

如果括号内可以包含其他内容,则您必须更具体,并且只在其中指定日期:

If it is possible to have something else within the parenthesis, you will have to be more specific and only specify a date inside:

"taskDate"\: newDate\("[a-zA-z]* \d{2}, \d{4} \d{2}\:\d{2}\:\d{2}"\)

这将匹配 "taskDate"\: newDate\("Letters 00, 00:00:00"\) 类型的所有内容.从这里您可以指定月份,并在所有引用之间留出空格.所有这些更改都使正则表达式更加复杂,因此只能使其更加严格以避免匹配您不想要的东西.如果不存在括号内可以包含其他内容的情况,我会使用第一个正则表达式.

This will match everything of the sort of "taskDate"\: newDate\("Letters 00, 00:00:00"\). From here you can either make the months specific, and allow white space between all the quotations. All these changes makes the regex more complex, so only make it more strict to avoid matching things that you don't want. If no scenario exist that something else can be inside the parenthesis, I would go with the first regex.

也就是说,在匹配内容中的字符串后,您需要编写代码将其用引号括起来.

That said, after you match the string within your content, you need to write the code to surround it with the quotations.

这篇关于如何使用正则表达式在语句周围加上引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆