# 读取发电机功角行向量 withopen(data_folder + "\\" + out_file, 'r', encoding='gbk') as f: find_flag = False for line in f: line = line.strip() if line.find('发电机') >= 0and line.find('输出数据列表') >= 0: motor_index = int(re.findall(r"\d+\.?\d*", line)[0]) if (motor_number.__contains__(motor_index)): find_flag = True continue if find_flag: line_data = [x.strip() for x in re.split(r" +", line)] iflen(line_data) < 8ornot is_all_float(line_data): continue line_data = [float(x) for x in line_data] # 寻找故障停止时间的功角 for i inrange(0, len(line_data), 2): time = line_data[i] power_angle = line_data[i + 1]
if time == fault_time: fault_power_angle.append(power_angle) find_flag = False break
r_resistance_matrix = [[0] * row for c inrange(col)] x_resistance_matrix = [[0] * row for c inrange(col)]
withopen(data_folder + "\\" + lis_file, 'r', encoding='gbk') as f: for line in f: line = line.strip().replace("\"", "") line_data = [x.strip() for x in re.split(r" +", line)] if line_data[0] != "L": continue data_from = motor_dict[int(re.sub(r"\D", "", line_data[1]))] data_to = motor_dict[int(re.sub(r"\D", "", line_data[3]))] r = float(line_data[-4]) x = float(line_data[-3]) r_resistance_matrix[data_from][data_to] = r x_resistance_matrix[data_from][data_to] = x
for i inrange(row): for j inrange(col): r_resistance_matrix[j][i] = r_resistance_matrix[i][j] x_resistance_matrix[j][i] = x_resistance_matrix[i][j]
withopen(data_folder + "\\" + swi_file, 'r', encoding='gbk') as f: for line in f: line = line.strip().replace("\"", "") line_data = [x.strip() for x in re.split(r" +", line)] if line_data[0] != "MF": continue node_voltage.append(float(line_data[2])) motivate_energy.append(float(line_data[3]))
Stringtext="This is the text to be searched " + "for occurrences of the http:// pattern."; Stringpattern=".*http://.*"; booleanmatches= Pattern.matches(pattern, text); System.out.println("matches = " + matches);ches = " + matches);
Stringtext= "This is the text to be searched for occurrences of the pattern."; Stringpattern=".*is.*"; booleanmatches= Pattern.matches(pattern, text); System.out.println("matches = " + matches); // true
Stringtext= "This is the text to be searched " + "for occurrences of the http:// pattern."; StringpatternString=".*http://.*"; Patternpattern= Pattern.compile(patternString);
Stringtext= "This is the text to be searched " + "for occurrences of the http:// pattern."; StringpatternString=".*http://.*"; Patternpattern= Pattern.compile(patternString, Pattern.CASE_INSENSITIVE); Matchermatcher= pattern.matcher(text); booleanmatches= matcher.matches(); System.out.println("matches = " + matches);
Stringtext="This is the text to be searched for occurrences of the http:// pattern."; StringpatternString=".*http://.*"; Patternpattern= Pattern.compile(patternString); Matchermatcher= pattern.matcher(text); booleanmatches= matcher.matches();
Stringtext= "This is the text to be searched " + "for occurrences of the http:// pattern."; StringpatternString=".*http://.*"; Patternpattern= Pattern.compile(patternString); Matchermatcher= pattern.matcher(text);
Stringtext= "This is the text to be searched " + "for occurrences of the http:// pattern."; StringpatternString="This is the"; Patternpattern= Pattern.compile(patternString, Pattern.CASE_INSENSITIVE); Matchermatcher= pattern.matcher(text); System.out.println("lookingAt = " + matcher.lookingAt()); System.out.println("matches = " + matcher.matches());
上面的例子分别对文本开头和整个文本匹配正则表达式 “this is the”. 匹配文本开头的方法lookingAt() 返回true。
对整个文本匹配正则表达式的方法 matches() 返回false,因为整个文本包含多余的字符,而 正则表达式要求文本精确匹配"this is the",前后又不能有额外字符。
Stringtext= "This is the text which is to be searched " + "for occurrences of the word 'is'."; StringpatternString="is"; Patternpattern= Pattern.compile(patternString); Matchermatcher= pattern.matcher(text); intcount=0; while(matcher.find()) { count++; System.out.println("found: " + count + " : " + matcher.start() + " - " + matcher.end()); }
Stringtext="John writes about this, and John writes about that," + " and John writes about everything. " ; StringpatternString1="(John)"; Patternpattern= Pattern.compile(patternString1); Matchermatcher= pattern.matcher(text); while(matcher.find()) { System.out.println("found: " + matcher.group(1)); }
Stringtext= "John writes about this, and John Doe writes about that," + " and John Wayne writes about everything." ; StringpatternString1="(John) (.+?) "; Patternpattern= Pattern.compile(patternString1); Matchermatcher= pattern.matcher(text); while(matcher.find()) { System.out.println("found: " + matcher.group(1) + " " + matcher.group(2)); }
注意代码中引用分组的方式。代码输出如下
1 2 3
found: John writes found: John Doe found: John Wayne
Stringtext= "John writes about this, and John Doe writes about that," + " and John Wayne writes about everything." ; StringpatternString1="((John) (.+?)) "; Patternpattern= Pattern.compile(patternString1); Matchermatcher= pattern.matcher(text); while(matcher.find()) { System.out.println("found: "); }
Stringtext= "John writes about this, and John Doe writes about that," + " and John Wayne writes about everything." ; StringpatternString1="((John) (.+?)) "; Patternpattern= Pattern.compile(patternString1); Matchermatcher= pattern.matcher(text); StringreplaceAll= matcher.replaceAll("Joe Blocks "); System.out.println("replaceAll = " + replaceAll); StringreplaceFirst= matcher.replaceFirst("Joe Blocks "); System.out.println("replaceFirst = " + replaceFirst);
输出如下
1 2 3 4
replaceAll = Joe Blocks about this, and Joe Blocks writes about that, and Joe Blocks writes about everything. replaceFirst = Joe Blocks about this, and John Doe writes about that, and John Wayne writes about everything.
输出中的换行和缩进是为了可读而增加的。
注意第1个字符串中所有出现 John 后跟一个单词 的地方,都被替换为 Joe Blocks 。第2个字符串中,只有第一个出现的被替换。
Stringtext= "John writes about this, and John Doe writes about that," + " and John Wayne writes about everything." ; StringpatternString1="((John) (.+?)) "; Patternpattern= Pattern.compile(patternString1); Matchermatcher= pattern.matcher(text); StringBufferstringBuffer=newStringBuffer(); while(matcher.find()){ matcher.appendReplacement(stringBuffer, "Joe Blocks "); System.out.println(stringBuffer.toString()); } matcher.appendTail(stringBuffer); System.out.println(stringBuffer.toString());
Joe Blocks Joe Blocks about this, and Joe Blocks Joe Blocks about this, and Joe Blocks writes about that, and Joe Blocks Joe Blocks about this, and Joe Blocks writes about that, and Joe Blocks writes about everything.