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 ")])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?