8 Replies - 373 Views - Last Post: 20 July 2013 - 07:42 AM Rate Topic: -----

#1 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Regex.Replace

Posted 18 July 2013 - 07:41 AM

I'm trying to replace a string that has the following with the string literal "s"

string _type = "movieLogic"

pattern = @"Logic | \b | [A-Z]|[0-9] | \D |\W";

so my pattern i'm attempting will replace string _type if it has "Logic", space,capaitalLetters (but ignore first character)
, any numbers, any symbols within string i.e. _ - / @ etc.

"movieLogic" "Movie121"
"Movie Logic" "Movielogic"
"Movie_Logic"

should all be replaced by movies.


string newKeyName = Regex.Replace(_type,pattern,"s",RegexOptions.IgnoreCase); 




so in this instance I want newkeyname to be "movies"

the regular expression I attempted isn't working

any ideas?

This post has been edited by ray1234: 18 July 2013 - 08:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Regex.Replace

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 313
  • View blog
  • Posts: 490
  • Joined: 13-June 12

Re: Regex.Replace

Posted 18 July 2013 - 07:47 AM

I highly recommend Expresso as a way to build and test regular expressions.
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4529
  • View blog
  • Posts: 7,899
  • Joined: 08-June 10

Re: Regex.Replace

Posted 18 July 2013 - 07:57 AM

I've read and re-read your post half a dozen times, and I honestly have no idea what your actual goal is. If you could clarify it better. Maybe provide several samples and the results you want to achieve from these samples, we might be able to help you better.

Also, I personally suggest using http://rubular.com to help you build your expressions; it's for Ruby so it's not a perfect match with C#'s regex, but it's close enough for most usages, and it's online.
Was This Post Helpful? 0
  • +
  • -

#4 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: Regex.Replace

Posted 18 July 2013 - 08:08 AM

View PostCurtis Rutland, on 18 July 2013 - 02:57 PM, said:

I've read and re-read your post half a dozen times, and I honestly have no idea what your actual goal is. If you could clarify it better. Maybe provide several samples and the results you want to achieve from these samples, we might be able to help you better.

Also, I personally suggest using http://rubular.com to help you build your expressions; it's for Ruby so it's not a perfect match with C#'s regex, but it's close enough for most usages, and it's online.


ignoring the first character.

if pattern finds a space, capital letter (ignoring first letter), numbers, symbols, or the word "logic"

in the string than replaces it with "s"

examples:

"MovieLogic" "Movie121"
"Movie Logic" "Movielogic"
"Movie_Logic"

replaces with Movies

This post has been edited by ray1234: 18 July 2013 - 08:10 AM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3620
  • View blog
  • Posts: 11,276
  • Joined: 05-May 12

Re: Regex.Replace

Posted 18 July 2013 - 12:37 PM

You need to look more closely at the substitutions and capture constructs: http://msdn.microsof...y/az24scfc.aspx

Without giving away the farm, the general approach you want is to capture the the word "Movie" followed by a group that has the patterns that you want to replace with "s". Then for the substitute string, you put in the token you have for the first capture group followed by an 's'.
Was This Post Helpful? 0
  • +
  • -

#6 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: Regex.Replace

Posted 20 July 2013 - 06:32 AM

I want to replace all characters after either Movie or Games with "s"

example

MovieLogic = Movies
GameLLL1 = Games
MovieSearchLogic = Movies
Movie__Logic = Movies

etc

heres my pattern so far = @"searchLogic|\D[A-Z]+\w\b|[0-9]+\b";


string newKeyName = Regex.Replace(_type.Name, pattern, "s", RegexOptions.IgnoreCase);


thanks,

Ray

This post has been edited by ray1234: 20 July 2013 - 06:34 AM

Was This Post Helpful? 0
  • +
  • -

#7 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: Regex.Replace

Posted 20 July 2013 - 06:59 AM

if (input.ToLower().StartsWith("movie")) return "Movies";
else if (input.ToLower().StartsWith("game")) return "Games";

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3611
  • View blog
  • Posts: 12,419
  • Joined: 12-December 12

Re: Regex.Replace

Posted 20 July 2013 - 07:10 AM

I'm with Curtis. Your current attempts to describe your question are unclear. You need to provide sample input together with the output that you want to obtain for these inputs.
Was This Post Helpful? 0
  • +
  • -

#9 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: Regex.Replace

Posted 20 July 2013 - 07:42 AM

View PostWitchking, on 20 July 2013 - 01:59 PM, said:

if (input.ToLower().StartsWith("movie")) return "Movies";
else if (input.ToLower().StartsWith("game")) return "Games";


thanks for this code, it made me look at my issue differently instead of using Regex. I solved the issue with

string newTypeName = typeName.Remove(typeName.Length - 1);

type.Key.Contains(newtypeName)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1