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 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能