Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can I get the index of immediate before special characters of a word in c#

How can I get the index of immediate before special characters of a word in c#

Scheduled Pinned Locked Moved C#
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rohit kakria
    wrote on last edited by
    #1

    My question is: I want to know the index of /* and */ coming immediate before alpha2 Example: hello world /* this is */ right way to get the index of /* track to */ alpha2 dummy text alpha dummy /* test */textalpha dummy text Thanks in advance

    Richard DeemingR 1 Reply Last reply
    0
    • R rohit kakria

      My question is: I want to know the index of /* and */ coming immediate before alpha2 Example: hello world /* this is */ right way to get the index of /* track to */ alpha2 dummy text alpha dummy /* test */textalpha dummy text Thanks in advance

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      A Regular Expression[^] is probably the simplest way:

      string input = @"hello world /* this is */ right way to get the index of /* track to */ alpha2 dummy text alpha dummy /* test */textalpha dummy text";
      Regex pattern = new Regex(@"\/\*[^\*\/]*\*\/\s+alpha2");
      Match match = pattern.Match(input);
      if (match.Success)
      {
      int theIndex = match.Index;
      ...
      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      R 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        A Regular Expression[^] is probably the simplest way:

        string input = @"hello world /* this is */ right way to get the index of /* track to */ alpha2 dummy text alpha dummy /* test */textalpha dummy text";
        Regex pattern = new Regex(@"\/\*[^\*\/]*\*\/\s+alpha2");
        Match match = pattern.Match(input);
        if (match.Success)
        {
        int theIndex = match.Index;
        ...
        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        R Offline
        R Offline
        rohit kakria
        wrote on last edited by
        #3

        Thanks Homer, Its good but is there any way without the regular expression as I have to search from a file and there are a lot of same keywords like alpha2. with this regular expression It will be difficult to find specific instance and its immediate before characters. Actually I have to track the keywords which are not coming in comments section. Not coming in /* */ and not started with //. comment may be of many lines. Can you please suggest the solution. Thanks in advance. Rohit

        L 1 Reply Last reply
        0
        • R rohit kakria

          Thanks Homer, Its good but is there any way without the regular expression as I have to search from a file and there are a lot of same keywords like alpha2. with this regular expression It will be difficult to find specific instance and its immediate before characters. Actually I have to track the keywords which are not coming in comments section. Not coming in /* */ and not started with //. comment may be of many lines. Can you please suggest the solution. Thanks in advance. Rohit

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Use one of the methods described in the .NET String Class (System)[^].

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups