doukang1962 2018-12-07 21:20
浏览 62
已采纳

如何删除仅从特定单词的最后一个实例开始的整个字符串?

I am trying to scrape some data from an RSS link. I am just beginning this project; there will be some stuff with GUIs later. I am having trouble removing some stuff that I don't want showing on a particular line. In this case, I want everything after the last "at" gone, so that it only shows the Job Title.

I have tried replacing instances of the "at" string with an empty string, but that would also remove any instance of "a" followed by "t" from the string. I think I have to set up a map of words separated by white spaces (so strings.Fields() maybe?), and then set up a for loop that will replace the entire string starting from a word.

Code:

package main

import (
    "encoding/xml"
    "fmt"
    "log"
    "net/http"
    "strings"
)

type JobInfo struct{
    Title string `xml:"title"`
    Location string `xml:"location"`
    Company string `xml:"a10:author"`
    PostDate string `xml:"pubDate"`
    Description string `xml:"description"`
}

type Channel struct{
    Title string `xml:"title"`
    Link  string `xml:"link"`
    Desc  string `xml:"description"`
    Items []JobInfo `xml:"item"`
}

type Rss struct {
    Channel Channel `xml:"channel"`
} 

func main() {
    resp, err := http.Get("https://stackoverflow.com/jobs/feed?l=Bridgewater%2c+MA%2c+USA&u=Miles&d=100")
    if err != nil{
        log.Fatal(err)
        return
    }
    defer resp.Body.Close()

    rss := Rss{}

    decoder := xml.NewDecoder(resp.Body)
    err = decoder.Decode(&rss)
    if err != nil{
        log.Fatal(err)
        return
    }

    fmt.Printf("%v
", rss.Channel.Title)
    for i, item := range rss.Channel.Items{
        fmt.Printf("%v. Job Information:
", i+1)
        fmt.Printf("Title: %v
", item.Title)
        fmt.Printf("Location: %v
", item.Location)
        fmt.Printf("Company: %v
", item.Company)
        postdate := strings.Replace(item.PostDate, "Z", "", -1)
        fmt.Printf("Post Date: %v
", postdate)
        fmt.Printf("Description: %v
", item.Description)
    }
}

Output (1 example):

1. Job Information:
Title: Senior Web Engineer at Maark (Boston, MA)
Location: Boston, MA
Company: 
Post Date: Fri, 07 Dec 2018 20:21:34 
Description: <p>At MAARK, we are passionate about bringing innovation and advanced concepts to life and sweating every detail in order to create the best possible experience for our clients and their customers. We believe that every interaction and transaction a user has with a product should be designed and built to work for that individual.</p><p>The clients we work with span a host of verticals  ranging from large financial institutions to telecoms to hospitality to bio-engineering. And the problems we solve for them range from highly animated, interactive frontends, to complex business rules on the backend, to IoT solutions that bridge digital and real worlds. We love diving into new subjects and researching our clients' businesses and look to work with people who are just as naturally curious.<br></p><p>In this role, you will work as part of our growing development team to craft and build frontend side of apps and web sites. We work on highly creative projects, utilize a wide variety of fullstack technologies, and empower each of our developers to create innovative solutions and explore and master emerging web technologies.</p><p>We are looking for local (Boston metro area) candidates only. No remote opportunities for this position. </p><br><p><strong>Benefits</strong></p><br><p><strong>About Maark</strong><br></p><p>Maark is a strategic marketing and innovation agency for global companies - headquartered in Boston, MA. We help our clients define and articulate their vision, design new connected customer experiences, and develop applications at the intersection of where whats possible meets whats relevant. </p><p>We are proud to foster a workplace free from discrimination. We strongly believe that diversity of experience, perspectives, and background will lead to a better environment for our employees and a better experience for our clients.</p><p><em>Maark is an Equal Employment Opportunity (EEO) employer. It is the policy of Maark to prohibit discrimination and harassment of any type and to afford equal employment opportunities to all persons without regard to race, color, religion, sex, national origin, age, gender, physical or mental disability, veteran-status, or any other characteristic protected by applicable federal, state or local law.</em></p>

In the Title field, this should only say: Senior Web Engineer

I will figure out the company name and the bytestring in the description later, but if anyone has any input on that, it would be appreciated!

  • 写回答

1条回答 默认 最新

  • doulandai0641 2018-12-07 22:10
    关注

    hope this works for you

       fmt.Println(item.Title[:strings.LastIndex(item.Title," at ")])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵