How do I add an 's' to the end of an output sentence? [closed]
How would I add a character to the end of my output? I am scraping some data, I am using a regex to get what I want. I got it but I have to add an 's' to the end of the sentence.
The sentence changes every time so I can't just do a replace. I think I am using the .NET regex engine.
If you simply want to append a "s" to the string, just use newstring = oldstring + "s"
.
If you need to add the s before the punctuation characters at the end of the sentence, you could search for (?=\W+$)
and replace that with s
. I can provide a code snippet if you say which language you're using. However, this assumes punctuation to be present (it won't match if the strings ends in an alphanumeric character).
In VB.net, for example:
ResultString = Regex.Replace(SubjectString, "(?=\W+$)", "s")
If you need to add an "s" at the end of the string, before any punctuation if present, then you can search for (?=\W+$)|(?<!\W)$
and replace with s
.
As an annotated multiline regex:
(?=\W+$) # match a position before one or more non-word characters at the end of the string
| # or
(?<!\W)$ # match the position at the end of the string unless it's preceded by a non-word character
If you need to do something else, please rephrase your question and give some examples.
精彩评论