优化这段代码private static final Pattern P_UNQUOTED_ATTRIBUTES = Pattern.compile("([a-z0-9]+)(=)([^\"\\s']+)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL)
时间: 2024-02-03 15:12:07 浏览: 68
这段代码的主要作用是匹配HTML标签中非引号包裹的属性,其正则表达式为`([a-z0-9]+)(=)([^\"\\s']+)`。
为了优化这段代码,可以考虑以下几个方面:
1. 编译正则表达式对象一次后重复使用:将Pattern对象定义成静态变量,避免每次调用时都重新编译一次正则表达式。
2. 使用预编译的字符类:将`[a-z0-9]`替换成`\w`,表示匹配所有字母和数字字符。
3. 使用引号限定符:将`[^\"\\s']+`替换成`\"([^\"]*)\"|'([^']*)'`,表示匹配双引号或单引号包裹的属性值。
优化后的代码如下所示:
```
private static final Pattern P_UNQUOTED_ATTRIBUTES = Pattern.compile("\\w+=(\"([^\"]*)\"|'([^']*)')", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
```
相关问题
Parse error. Expected a command name, got unquoted argument with text [cmake] "-DCMAKE_VERBOSE_MAKEFILE=ON".
这个错误信息通常是因为在 CMakeLists.txt 文件中使用了不被支持的语法或者命令。根据错误信息,似乎是在 CMakeLists.txt 文件中使用了类似于 `[cmake] "-DCMAKE_VERBOSE_MAKEFILE=ON"` 的不带引号的参数。
请检查你的 CMakeLists.txt 文件是否存在类似的语句,如果有,请确保将参数用引号括起来,例如:`"[cmake] -DCMAKE_VERBOSE_MAKEFILE=ON"`。
private final CharSequence getSmallTime() { Context context = getContext(); boolean is24 = DateFormat.is24HourFormat(context); LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); final char MAGIC1 = '\uEF00'; final char MAGIC2 = '\uEF01'; SimpleDateFormat sdf; String format = is24 ? d.timeFormat24 : d.timeFormat12; if (!format.equals(mClockFormatString)) { /* * Search for an unquoted "a" in the format string, so we can * add dummy characters around it to let us find it again after * formatting and change its size. */ if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) { int a = -1; boolean quoted = false; for (int i = 0; i < format.length(); i++) { char c = format.charAt(i); if (c == ''') { quoted = !quoted; } if (!quoted && c == 'a') { a = i; break; } } if (a >= 0) { // Move a back so any whitespace before AM/PM is also in the alternate size. final int b = a; while (a > 0 && Character.isWhitespace(format.charAt(a-1))) { a--; } format = format.substring(0, a) + MAGIC1 + format.substring(a, b) + "a" + MAGIC2 + format.substring(b + 1); } } mClockFormat = sdf = new SimpleDateFormat(format); mClockFormatString = format; } else { sdf = mClockFormat; } String result = sdf.format(mCalendar.getTime()); if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) { int magic1 = result.indexOf(MAGIC1); int magic2 = result.indexOf(MAGIC2); if (magic1 >= 0 && magic2 > magic1) { SpannableStringBuilder formatted = new SpannableStringBuilder(result); if (AM_PM_STYLE == AM_PM_STYLE_GONE) { formatted.delete(magic1, magic2+1); } else { if (AM_PM_STYLE == AM_PM_STYLE_SMALL) { CharacterStyle style = new RelativeSizeSpan(0.7f); formatted.setSpan(style, magic1, magic2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); } formatted.delete(magic2, magic2 + 1); formatted.delete(magic1, magic1 + 1); } return formatted; } } return result; }添加秒
你的问题是如何在这段代码中添加秒?
这段代码的作用是获取当前时间并以指定格式显示出来,但没有显示秒数。要添加秒数,可以修改格式字符串。在这里,我们可以在格式字符串的末尾添加一个”:ss“,表示显示秒数。修改后的代码如下:
```
private final CharSequence getSmallTime() {
Context context = getContext();
boolean is24 = DateFormat.is24HourFormat(context);
LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
final char MAGIC1 = '\uEF00';
final char MAGIC2 = '\uEF01';
SimpleDateFormat sdf;
String format = is24 ? d.timeFormat24 : d.timeFormat12;
// Add seconds to the format string
format += ":ss";
if (!format.equals(mClockFormatString)) {
/*
* Search for an unquoted "a" in the format string, so we can
* add dummy characters around it to let us find it again after
* formatting and change its size.
*/
if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
int a = -1;
boolean quoted = false;
for (int i = 0; i < format.length(); i++) {
char c = format.charAt(i);
if (c == '\'') {
quoted = !quoted;
}
if (!quoted && c == 'a') {
a = i;
break;
}
}
if (a >= 0) {
// Move a back so any whitespace before AM/PM is also in the alternate size.
final int b = a;
while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
a--;
}
format = format.substring(0, a) + MAGIC1 + format.substring(a, b) + "a" + MAGIC2 + format.substring(b + 1);
}
}
mClockFormat = sdf = new SimpleDateFormat(format);
mClockFormatString = format;
} else {
sdf = mClockFormat;
}
String result = sdf.format(mCalendar.getTime());
if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
int magic1 = result.indexOf(MAGIC1);
int magic2 = result.indexOf(MAGIC2);
if (magic1 >= 0 && magic2 > magic1) {
SpannableStringBuilder formatted = new SpannableStringBuilder(result);
if (AM_PM_STYLE == AM_PM_STYLE_GONE) {
formatted.delete(magic1, magic2+1);
} else {
if (AM_PM_STYLE == AM_PM_STYLE_SMALL) {
CharacterStyle style = new RelativeSizeSpan(0.7f);
formatted.setSpan(style, magic1, magic2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
formatted.delete(magic2, magic2 + 1);
formatted.delete(magic1, magic1 + 1);
}
return formatted;
}
}
return result;
}
```
在修改后的代码中,我们在格式字符串的末尾添加了一个冒号和“ss”,表示显示秒数。然后,我们将修改后的格式字符串传递给SimpleDateFormat对象,以便格式化当前时间。最后,我们返回格式化后的时间字符串,它包含了秒数。
阅读全文