tags:

views:

78

answers:

1

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.

+3  A: 

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.

Tim Pietzcker
this is my regex codeWholesalereplacehome > replace > replace :this is what iam using it onHome > Wholesale Cell Phones > Wholesale HiPhonethis changes everytime to something newhere is my output thus far Cell Phones: HiPhonenow i need to add an S to the end of it but becouse this changes everytie i cant just do hiphone replaces hiphones
kyle
Sure, but can't you maniplate the string you read in before you write it out to your page? You can then use the `+ "s"` on your regex output?
Rup