最后连接子字符串的 tcl 字符串或正则表达式

发布于 2025-01-11 00:25:04 字数 612 浏览 0 评论 0原文

目前,我正在删除第一个位置子字符串,但我想在文件中提到的所有字符串的最后一个位置加入相同的第一个位置子字符串 (注意:这里每个子字符串都按下划线分类)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2025-01-18 00:25:04

有很多方法可以做到这一点。这是一种方法,将输入字符串转换为由“_”分隔的元素列表,重新排列列表,然后转换回字符串形式:

set schl [split $school _]
set schl [lassign $schl first]
lappend schl $first
set school [join $schl _]

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:

set schl [split $school _]
set schl [lassign $schl first]
lappend schl $first
set school [join $schl _]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文