d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Regex-help > Short Question
12Next
Add Reply New Topic New Poll
Member
Posts: 22,269
Joined: May 26 2007
Gold: 100.24
Feb 27 2013 12:12am
I need to create a regex that gives me string from fifth last character to ninth last character. For example

$a = test string 123 I need this piglet went to shower
$b = test string test string 123 I need this piglet went to shower

I need to get the "I need this" part. Problem is that the beginning of the line is gibberish and can come in various lengths.
Member
Posts: 568
Joined: Jan 17 2007
Gold: 0.00
Feb 27 2013 12:45am
Does this need to be Regex?

Regex is for string matching not slicing
Member
Posts: 22,269
Joined: May 26 2007
Gold: 100.24
Feb 27 2013 06:21am
Quote (Jyth @ 27 Feb 2013 08:45)
Does this need to be Regex?

Regex is for string matching not slicing


Anything goes within powershell, I thought that regex would be the most modifiable for the future use.
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Feb 27 2013 07:09am
Quote (Deppi @ Feb 26 2013 11:12pm)
I need to create a regex that gives me string from fifth last character to ninth last character. For example

$a = test string 123 I need this piglet went to shower
$b = test string test string 123 I need this piglet went to shower

I need to get the "I need this" part. Problem is that the beginning of the line is gibberish and can come in various lengths.


can you give us some real examples of what the strings look like?
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Feb 27 2013 07:38am
Quote (Jyth @ Feb 27 2013 01:45am)
Does this need to be Regex?

Regex is for string matching not slicing


So what are capture groups for?
Member
Posts: 22,269
Joined: May 26 2007
Gold: 100.24
Feb 27 2013 11:09pm
Quote (Azrad @ 27 Feb 2013 15:09)
can you give us some real examples of what the strings look like?


Of course :)


1361936079 account 1103066 removed
1361849622 account TEXTACC removed
1361849661 account 1102605 removed


The first number is some sort of line number I guess, so in future it might be longer thus I can't capture from the beginning. Rest is always in same format, except that the desired string (cursive in this example, not in real format) can be numbers or capital letters. All the examples I have seen this far have been 7 characters long.
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Feb 27 2013 11:35pm
Not familiar with regex at all so I'm sorry if this doesn't help you
But

Just find first space
Then second space
Then third space

Then do your slicing from those lengths

Ie
string = left(string, third_space)
string = right(string, second_space)
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 28 2013 01:08am
can just split by space and store into an array. first thing that comes to mind is this

Code
int main()
{
   char userInput[81];
   char *functionOutput[80];
   int i = 0;


   printf("Enter a string to test the split function\n");
   fgets(userInput, sizeof(userInput), stdin);

   split(userInput, ".", functionOutput);

   while(functionOutput[i] != '\0')
   {
       printf("%s\n", functionOutput[i]);
       i++;
   }

   getchar();
   return 0;

}



void split(char *input, char *dilimeter, char **output)
{
   int i = 0;

   output[0] = strtok(input, dilimeter);

   while(output[i] != NULL)
   {
       i++;
       output[i] = strtok(NULL, dilimeter);
   }

   output[i + 1] = '\0';
}


you dont necessarily need regex. see if your language has has string functions. python has a net set iirc.

could also do something like this.

Code
char *pch, *pch2;

pch = data;
pch2 = data;

while(*pch)
if(*pch == ' ')
  memcopy(newdata, data, pch - pch2);
  pch2 = pch;


or something like that

This post was edited by AbDuCt on Feb 28 2013 01:13am
Member
Posts: 22,269
Joined: May 26 2007
Gold: 100.24
Feb 28 2013 03:03am
Thank you everyone, your opinions opened new point of views to the problem. I decided to use string functions of Powershell to get to my result (or at least for now, might change in future). Just wanted to see how it would work in powershell since it's more "universal" if you know what I mean.
Member
Posts: 1
Joined: Feb 27 2013
Gold: 0.00
Feb 28 2013 03:13am
Quote (rockonkenshin @ Feb 27 2013 05:38am)
So what are capture groups for?


Capture groups are for capturing MATCHES. There is no way to match string character indices with a Regex. And there is no neet to, you can just read the characters with the indices.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll