regular expressions help

  • Thread starter Thread starter supercrossking
  • Start date Start date
S

supercrossking

I am trying to the values of string of text in the sample before. The
ds are for digits and s is for string and string of text is for a
string with more than one or two values. I am trying to use regex and
the .groups method. Please help.

d|d|d|string of text 1||s|s|||dd.dd|ss|string of text
2||||||||||||||||||||||||||string of text 2

I only want string of text1
 
I want to grab the 4th item from the entire string of this
d|d|d|string of text 1||s|s|||dd.dd|ss|string of text
2||||||||||||||||||||||||||string of text 2
1|1|1|ABCDEF.....XYZ 1||A|A|||11.11|AB|ABCDEF.....XYZ
2||||||||||||||||||||||||||ABCDEF.....XYZ 2

I only need string of text 1 for display, not d|d|d|string of text 1||
s|s|||dd.dd|ss|string of text 2||||||||||||||||||||||||||string of
text 2.
 
I am not sure I understand the question, but I think this will help.

You can create a named group by enclosing it in paranethsis and add ?<name>.

For example, I have the following Regex pattern in my code
"_(?<WellName>[A-H]\\d{1,2})_"

This is used to capture just the Well Designation (a letter from A-H and
then 1 or 2 digits) from a longer string as long as that pattern is
surrounded by underscores.


Regex.Match("SomeText_B7_MoreText","_(?<WellName>[A-H]\\d{1,2})_").Groups["WellName"].Value;

Would return "B7" (If I don't have a typo somewhere...)
Ethan
 
I will try to make it more clear.

1|2|3|this are is the text I want to display||a|b|||45.67|cd|don't
need this text
|||||||||||||||||||||||||| this text

I've tried some combinations of things to get exactly what I want, but
can't get it 100%.

([\w.]+\s*)?,?").Groups(1).ToString() only gets first item, number 1
in this case. I want the text "this are is the text I want to display"
and only that. The pattern is just like it is above, the first 3 are
digits, then some text(the text I want), empty, one alpha character,
etc in example.

I am not sure I understand the question, but I think this will help.

You can create a named group by enclosing it in paranethsis and add ?<name>.

For example, I have the following Regex pattern in my code
"_(?<WellName>[A-H]\\d{1,2})_"

This is used to capture just the Well Designation (a letter from A-H and
then 1 or 2 digits) from a longer string as long as that pattern is
surrounded by underscores.

Regex.Match("SomeText_B7_MoreText","_(?<WellName>[A-H]\\d{1,2})_").Groups["­WellName"].Value;

Would return "B7" (If I don't have a typo somewhere...)
Ethan



I am trying to the values of string of text in the sample before. The
ds are for digits and s is for string and string of text is for a
string with more than one or two values. I am trying to use regex and
the .groups method. Please help.
d|d|d|string of text 1||s|s|||dd.dd|ss|string of text
2||||||||||||||||||||||||||string of text 2
I only want string of text1- Hide quoted text -

- Show quoted text -
 
This is for a pipe delimited string of text using regular expressions.

I will try to make it more clear.

1|2|3|this are is the text I want to display||a|b|||45.67|cd|don't
need this text
|||||||||||||||||||||||||| this text

I've tried some combinations of things to get exactly what I want, but
can't get it 100%.

([\w.]+\s*)?,?").Groups(1).ToString() only gets first item, number 1
in this case. I want the text "this are is the text I want to display"
and only that. The pattern is just like it is above, the first 3 are
digits, then some text(the text I want), empty, one alpha character,
etc in example.

I am not sure I understand the question, but I think this will help.
You can create a named group by enclosing it in paranethsis and add ?<name>.
For example, I have the following Regex pattern in my code
"_(?<WellName>[A-H]\\d{1,2})_"
This is used to capture just the Well Designation (a letter from A-H and
then 1 or 2 digits) from a longer string as long as that pattern is
surrounded by underscores.
Regex.Match("SomeText_B7_MoreText","_(?<WellName>[A-H]\\d{1,2})_").Groups["­­WellName"].Value;

Would return "B7" (If I don't have a typo somewhere...)
Ethan
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
So, are you trying to get an specific regular expression to use? The way I
generally start is by describing exactly what about the pattern is unique. In
this case, I think what you want is the first set of characters which does
not contain any pipes (|) after 3 occurances of 1 digit 1 pipe. Is that
correct?

If so, try this

(\d|){3}(?<TheGroupIWant>.*)|

Here is what that means

(\d|){3} 3 occurances of any digit followed by a pipe
(?<TheGroupIWant>.*) any number of characters (except new
lines) captured into a group named "TheGroupIWant"
| A pipe (so it stops this group as soon as a
pipe occurs. )

Ethan




This is for a pipe delimited string of text using regular expressions.

I will try to make it more clear.

1|2|3|this are is the text I want to display||a|b|||45.67|cd|don't
need this text
|||||||||||||||||||||||||| this text

I've tried some combinations of things to get exactly what I want, but
can't get it 100%.

([\w.]+\s*)?,?").Groups(1).ToString() only gets first item, number 1
in this case. I want the text "this are is the text I want to display"
and only that. The pattern is just like it is above, the first 3 are
digits, then some text(the text I want), empty, one alpha character,
etc in example.

I am not sure I understand the question, but I think this will help.
You can create a named group by enclosing it in paranethsis and add ?<name>.
For example, I have the following Regex pattern in my code
"_(?<WellName>[A-H]\\d{1,2})_"
This is used to capture just the Well Designation (a letter from A-H and
then 1 or 2 digits) from a longer string as long as that pattern is
surrounded by underscores.
Regex.Match("SomeText_B7_MoreText","_(?<WellName>[A-H]\\d{1,2})_").Groups["­­WellName"].Value;

Would return "B7" (If I don't have a typo somewhere...)
Ethan
:
I am trying to the values of string of text in the sample before. The
ds are for digits and s is for string and string of text is for a
string with more than one or two values. I am trying to use regex and
the .groups method. Please help.
d|d|d|string of text 1||s|s|||dd.dd|ss|string of text
2||||||||||||||||||||||||||string of text 2
I only want string of text1- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
That makes sense, but it does not work.

This is VB.net

Dim stringofText As String = "9|1|1|Today is a great day|1"
Response.Write( Regex.Match( stringofText, "(\d|){3}(?
<TheGroupIWant>.*)| ").Groups(1).ToString() )

I did get it to work be doing a split and an array. I wouldn't mind
figuring out the regex way to do it too.


So, are you trying to get an specific regular expression to use?  The way I
generally start is by describing exactly what about the pattern is unique.In
this case, I think what you want is the first set of characters which does
not contain any pipes (|) after 3 occurances of 1 digit 1 pipe. Is that
correct?

If so, try this

(\d|){3}(?<TheGroupIWant>.*)|

Here is what that means

(\d|){3}                   3 occurances of any digit followed by a pipe
(?<TheGroupIWant>.*)              any number of characters (except new
lines) captured into a group named "TheGroupIWant"
|                            A pipe (so it stops this group as soon as a
pipe occurs. )

Ethan



This is for a pipe delimited string of text using regular expressions.
I will try to make it more clear.
1|2|3|this are is the text I want to display||a|b|||45.67|cd|don't
need this text
|||||||||||||||||||||||||| this text
I've tried some combinations of things to get exactly what I want, but
can't get it 100%.
([\w.]+\s*)?,?").Groups(1).ToString() only gets first item, number 1
in this case. I want the text "this are is the text I want to display"
and only that. The pattern is just like it is above, the first 3 are
digits, then some text(the text I want), empty, one alpha character,
etc in example.
On Jan 9, 3:29 pm, Ethan Strauss
I am not sure I understand the question, but I think this will help.
You can create a named group by enclosing it in paranethsis and add ?<name>.
For example, I have the following Regex pattern in my code
"_(?<WellName>[A-H]\\d{1,2})_"
This is used to capture just the Well Designation (a letter from A-Hand
then 1 or 2 digits) from a longer string as long as that pattern is
surrounded by underscores.
Regex.Match("SomeText_B7_MoreText","_(?<WellName>[A-H]\\d{1,2})_").Groups["­­­WellName"].Value;
Would return "B7" (If I don't have a typo somewhere...)
Ethan
:
I am trying to the values of string of text in the sample before. The
ds are for digits and s is for string and string of text is for a
string with more than one or two values. I am trying to use regex and
the .groups method. Please help.
d|d|d|string of text 1||s|s|||dd.dd|ss|string of text
2||||||||||||||||||||||||||string of text 2
I only want string of text1- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
If I do

Dim stringofText As String = "9|1|1|Today is a great day|1"
Response.Write( Regex.Match( stringofText, "(\d|){3}(?
<TheGroupIWant>.*)| ").Groups("TheGroupIWant").ToString() )

I get 9|1|1|Today is a great day|1, not Today is a great day

So, are you trying to get an specific regular expression to use?  The way I
generally start is by describing exactly what about the pattern is unique.In
this case, I think what you want is the first set of characters which does
not contain any pipes (|) after 3 occurances of 1 digit 1 pipe. Is that
correct?

If so, try this

(\d|){3}(?<TheGroupIWant>.*)|

Here is what that means

(\d|){3}                   3 occurances of any digit followed by a pipe
(?<TheGroupIWant>.*)              any number of characters (except new
lines) captured into a group named "TheGroupIWant"
|                            A pipe (so it stops this group as soon as a
pipe occurs. )

Ethan



This is for a pipe delimited string of text using regular expressions.
I will try to make it more clear.
1|2|3|this are is the text I want to display||a|b|||45.67|cd|don't
need this text
|||||||||||||||||||||||||| this text
I've tried some combinations of things to get exactly what I want, but
can't get it 100%.
([\w.]+\s*)?,?").Groups(1).ToString() only gets first item, number 1
in this case. I want the text "this are is the text I want to display"
and only that. The pattern is just like it is above, the first 3 are
digits, then some text(the text I want), empty, one alpha character,
etc in example.
On Jan 9, 3:29 pm, Ethan Strauss
I am not sure I understand the question, but I think this will help.
You can create a named group by enclosing it in paranethsis and add ?<name>.
For example, I have the following Regex pattern in my code
"_(?<WellName>[A-H]\\d{1,2})_"
This is used to capture just the Well Designation (a letter from A-Hand
then 1 or 2 digits) from a longer string as long as that pattern is
surrounded by underscores.
Regex.Match("SomeText_B7_MoreText","_(?<WellName>[A-H]\\d{1,2})_").Groups["­­­WellName"].Value;
Would return "B7" (If I don't have a typo somewhere...)
Ethan
:
I am trying to the values of string of text in the sample before. The
ds are for digits and s is for string and string of text is for a
string with more than one or two values. I am trying to use regex and
the .groups method. Please help.
d|d|d|string of text 1||s|s|||dd.dd|ss|string of text
2||||||||||||||||||||||||||string of text 2
I only want string of text1- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
You have to escape the pipe with a \.

Dim stringofText As String = "9|1|1|Today is a great day.|1"
Response.Write( Regex.Match( stringofText, "(\d\|){3}(?
<TheGroupIWant>.*)\|").Groups("TheGroupIWant").ToString() )

That works just fine.


If I do

 Dim stringofText As String = "9|1|1|Today is a great day|1"
       Response.Write( Regex.Match( stringofText, "(\d|){3}(?
<TheGroupIWant>.*)| ").Groups("TheGroupIWant").ToString() )

I get 9|1|1|Today is a great day|1, not Today is a great day

So, are you trying to get an specific regular expression to use?  The way I
generally start is by describing exactly what about the pattern is unique. In
this case, I think what you want is the first set of characters which does
not contain any pipes (|) after 3 occurances of 1 digit 1 pipe. Is that
correct?
If so, try this

Here is what that means
(\d|){3}                   3 occurances of any digit followed by a pipe
(?<TheGroupIWant>.*)              any number of characters(except new
lines) captured into a group named "TheGroupIWant"
|                            A pipe (so it stops this group as soon as a
pipe occurs. )

This is for a pipe delimited string of text using regular expressions.
On Jan 10, 9:03 am, (e-mail address removed) wrote:
I will try to make it more clear.
1|2|3|this are is the text I want to display||a|b|||45.67|cd|don't
need this text
|||||||||||||||||||||||||| this text
I've tried some combinations of things to get exactly what I want, but
can't get it 100%.
([\w.]+\s*)?,?").Groups(1).ToString() only gets first item, number 1
in this case. I want the text "this are is the text I want to display"
and only that. The pattern is just like it is above, the first 3 are
digits, then some text(the text I want), empty, one alpha character,
etc in example.
On Jan 9, 3:29 pm, Ethan Strauss
I am not sure I understand the question, but I think this will help.
You can create a named group by enclosing it in paranethsis and add ?<name>.
For example, I have the following Regex pattern in my code
"_(?<WellName>[A-H]\\d{1,2})_"
This is used to capture just the Well Designation (a letter from A-H and
then 1 or 2 digits) from a longer string as long as that pattern is
surrounded by underscores.
Regex.Match("SomeText_B7_MoreText","_(?<WellName>[A-H]\\d{1,2})_")..Groups["­­­­WellName"].Value;
Would return "B7" (If I don't have a typo somewhere...)
Ethan
:
I am trying to the values of string of text in the sample before.. The
ds are for digits and s is for string and string of text is for a
string with more than one or two values. I am trying to use regex and
the .groups method. Please help.
d|d|d|string of text 1||s|s|||dd.dd|ss|string of text
2||||||||||||||||||||||||||string of text 2
I only want string of text1- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
It doesn't work with more data at the end, like this.

Dim stringofText As String = "4|1|3|have a nice day.||a|b|||12.34|ab|
abcdefg||||||||||||||||||||||||||ACBDEFG123456789"
Response.Write( Regex.Match( stringofText, "(\d\|){3}(?
<TheGroupIWant>.*)(\w?|\s?\|)?").Groups("TheGroupIWant").ToString() )


You have to escape the pipe with a \.

Dim stringofText As String = "9|1|1|Today is a great day.|1"
       Response.Write( Regex.Match( stringofText, "(\d\|){3}(?
<TheGroupIWant>.*)\|").Groups("TheGroupIWant").ToString() )

That works just fine.

 Dim stringofText As String = "9|1|1|Today is a great day|1"
       Response.Write( Regex.Match( stringofText, "(\d|){3}(?
<TheGroupIWant>.*)| ").Groups("TheGroupIWant").ToString() )
I get 9|1|1|Today is a great day|1, not Today is a great day
So, are you trying to get an specific regular expression to use?  The way I
generally start is by describing exactly what about the pattern is unique. In
this case, I think what you want is the first set of characters which does
not contain any pipes (|) after 3 occurances of 1 digit 1 pipe. Is that
correct?
If so, try this
(\d|){3}(?<TheGroupIWant>.*)|
Here is what that means
(\d|){3}                   3 occurances of any digitfollowed by a pipe
(?<TheGroupIWant>.*)              any number of characters (except new
lines) captured into a group named "TheGroupIWant"
|                            A pipe (so itstops this group as soon as a
pipe occurs. )
Ethan
:
This is for a pipe delimited string of text using regular expressions.
On Jan 10, 9:03 am, (e-mail address removed) wrote:
I will try to make it more clear.
1|2|3|this are is the text I want to display||a|b|||45.67|cd|don't
need this text
|||||||||||||||||||||||||| this text
I've tried some combinations of things to get exactly what I want,but
can't get it 100%.
([\w.]+\s*)?,?").Groups(1).ToString() only gets first item, number1
in this case. I want the text "this are is the text I want to display"
and only that. The pattern is just like it is above, the first 3 are
digits, then some text(the text I want), empty, one alpha character,
etc in example.
On Jan 9, 3:29 pm, Ethan Strauss
I am not sure I understand the question, but I think this will help.
You can create a named group by enclosing it in paranethsis and add ?<name>.
For example, I have the following Regex pattern in my code
"_(?<WellName>[A-H]\\d{1,2})_"
This is used to capture just the Well Designation (a letter fromA-H and
then 1 or 2 digits) from a longer string as long as that patternis
surrounded by underscores.
Regex.Match("SomeText_B7_MoreText","_(?<WellName>[A-H]\\d{1,2})_").Groups["­­­­­WellName"].Value;
Would return "B7" (If I don't have a typo somewhere...)
Ethan
:
I am trying to the values of string of text in the sample before. The
ds are for digits and s is for string and string of text is for a
string with more than one or two values. I am trying to use regex and
the .groups method. Please help.
d|d|d|string of text 1||s|s|||dd.dd|ss|string of text
2||||||||||||||||||||||||||string of text 2
I only want string of text1- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
Sorry,
I actually tested it this time, so it works for sure for me.

String text = "4|1|3|have a nice
day.||a|b|||12.34|ab|abcdefg||||||||||||||||||||||||||ACBDEFG123456789";
string Results = Regex.Match(text,
@"(\d\|){3}(?<TheGroupIWant>[^\|]*)").Groups["TheGroupIWant"].ToString();

Gives "have a nice day."

It looks like you are using VB, so your syntax is a little different, but
anyway, the "[^\|]" means Anything except a pipe.

Ethan

It doesn't work with more data at the end, like this.

Dim stringofText As String = "4|1|3|have a nice day.||a|b|||12.34|ab|
abcdefg||||||||||||||||||||||||||ACBDEFG123456789"
Response.Write( Regex.Match( stringofText, "(\d\|){3}(?
<TheGroupIWant>.*)(\w?|\s?\|)?").Groups("TheGroupIWant").ToString() )


You have to escape the pipe with a \.

Dim stringofText As String = "9|1|1|Today is a great day.|1"
Response.Write( Regex.Match( stringofText, "(\d\|){3}(?
<TheGroupIWant>.*)\|").Groups("TheGroupIWant").ToString() )

That works just fine.

Dim stringofText As String = "9|1|1|Today is a great day|1"
Response.Write( Regex.Match( stringofText, "(\d|){3}(?
<TheGroupIWant>.*)| ").Groups("TheGroupIWant").ToString() )
I get 9|1|1|Today is a great day|1, not Today is a great day
On Jan 10, 10:23 am, Ethan Strauss
So, are you trying to get an specific regular expression to use? The way I
generally start is by describing exactly what about the pattern is unique. In
this case, I think what you want is the first set of characters which does
not contain any pipes (|) after 3 occurances of 1 digit 1 pipe. Is that
correct?
If so, try this

Here is what that means
(\d|){3} 3 occurances of any digit followed by a pipe
(?<TheGroupIWant>.*) any number of characters (except new
lines) captured into a group named "TheGroupIWant"
| A pipe (so it stops this group as soon as a
pipe occurs. )

:
This is for a pipe delimited string of text using regular expressions.
On Jan 10, 9:03 am, (e-mail address removed) wrote:
I will try to make it more clear.
1|2|3|this are is the text I want to display||a|b|||45.67|cd|don't
need this text
|||||||||||||||||||||||||| this text
I've tried some combinations of things to get exactly what I want, but
can't get it 100%.
([\w.]+\s*)?,?").Groups(1).ToString() only gets first item, number 1
in this case. I want the text "this are is the text I want to display"
and only that. The pattern is just like it is above, the first 3 are
digits, then some text(the text I want), empty, one alpha character,
etc in example.
On Jan 9, 3:29 pm, Ethan Strauss
I am not sure I understand the question, but I think this will help.
You can create a named group by enclosing it in paranethsis and add ?<name>.
For example, I have the following Regex pattern in my code
"_(?<WellName>[A-H]\\d{1,2})_"
This is used to capture just the Well Designation (a letter from A-H and
then 1 or 2 digits) from a longer string as long as that pattern is
surrounded by underscores.
Regex.Match("SomeText_B7_MoreText","_(?<WellName>[A-H]\\d{1,2})_").Groups["­­­­­WellName"].Value;

Would return "B7" (If I don't have a typo somewhere...)
Ethan
:
I am trying to the values of string of text in the sample before. The
ds are for digits and s is for string and string of text is for a
string with more than one or two values. I am trying to use regex and
the .groups method. Please help.
d|d|d|string of text 1||s|s|||dd.dd|ss|string of text
2||||||||||||||||||||||||||string of text 2
I only want string of text1- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top