开发者

How do I add an 's' to the end of an output sentence? [closed]

It's difficult to tell what is being asked here. This开发者_运维问答 question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜