最后连接子字符串的 tcl 字符串或正则表达式
目前,我正在删除第一个位置子字符串,但我想在文件中提到的所有字符串的最后一个位置加入相同的第一个位置子字符串 (注意:这里每个子字符串都按下划线分类)
file.txt
A_Class
B_Class
A_Name_Student
B_Name_Student
Marks_Mid_Term_Student
Marks_Student_Subject
到目前为止,我尝试删除第一个子字符串,但最后无法加入它,
set school [string range $school [expr {[string first "_" $school] + 1}] end]
基本上,我想将其打印为
Class_A
Class_B
Name_Student_A
Name_Student_B
Mid_Term_Student_Marks
Student_Subject_Marks
Currently, I'm removing the first position substring, but I wanted to join the same first position substring at the last position for all the strings mentioned in the file
(Note: Here each substring is categorized by underscore)
file.txt
A_Class
B_Class
A_Name_Student
B_Name_Student
Marks_Mid_Term_Student
Marks_Student_Subject
so far I tried to remove the first substring but was unable to join it at last,
set school [string range $school [expr {[string first "_" $school] + 1}] end]
Basically, I wanted to print it as
Class_A
Class_B
Name_Student_A
Name_Student_B
Mid_Term_Student_Marks
Student_Subject_Marks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有很多方法可以做到这一点。这是一种方法,将输入字符串转换为由“_”分隔的元素列表,重新排列列表,然后转换回字符串形式:
There are lots of ways to do this. Here is one way, converting the input string to a list of the elements which were separated by "_", rearranging the list, then converting back to string form: