Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regular Expression

Posted on 2006-06-23
10
Medium Priority
?
426 Views
Last Modified: 2010-04-17
With the following regular expression what would be used to only select the carriage return at the end of the line...

A.{2}\r

In this scenario I would only want the \r selected but need to keep the beginning of the regular expression... not looking to find all carriage returns...



0
Comment
Question by:orionpm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 16974133
Usually the $ says match end of line, so this has to be added and it should match the last return.

Regards
Friedrich
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 16975236
It's not clear what you;re trying to do, but if you're trying to grab the piece before the end of the line, you could try:

    \(A.{2}\)$

and the stuff between \( and \) will be called \1. Of course, there are slight variations in all these different regular expression interpreters - where are you using this?
0
 
LVL 1

Author Comment

by:orionpm
ID: 16975646
This is in VBScript...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:orionpm
ID: 16975838
For example out of the following string I would only want to select 't'... The string that I have could be alpha numberic, white space, etc which is why I am using the .{}.

A  t
B  z
A  t
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 16975847
For VBScript, assuming you want to grab the part of the expression from the "A" followed by 2 chars at the end of the line, you can use:

      myRegexp = new RegExp
      myRegexp.Pattern = "^(.*)(A.{2})$"
      myRegexp.Global = True
      myRegexp.IgnoreCase = True

      Dim found
      Set found = myRegexp.test(inputString)
         ' OK, we found the pattern ...
         ' RegExp.$1 contains the first part of the line, and RegExp.$2 contains the second part ("part" is the piece enclosed in parentheses)
      End If
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 16975854
OK - just saw your example. The Pattern field above should be set to:
    "^.*(..)$"

and RegExp.$1 will be equal to the last 2 chars in the line
0
 
LVL 1

Author Comment

by:orionpm
ID: 16975888
When I use the $ no results are returned... I am looking to only return the t in my regular expression as I want to replace the t only (but this criteria is based on matching on the first part of the string first; I wouldn't want to remove all t's in the data)... ie.

Set objRegExp = CreateObject("VBScript.RegExp")
objRegExp.Pattern = "^(.*)(A.{2})$" 'Not sure at this point if I can select the t alone based on the first part of the string
objRegExp.Global = True
objRegExp.Multiline = True

strTEXT = objRegExp.Replace(strContents,"") 'At this point I would like the t only to be stored in objRegExp so that it can be removed
0
 
LVL 15

Accepted Solution

by:
bpmurray earned 2000 total points
ID: 16975927
     myRegexp = new RegExp
      myRegexp.Pattern = "^.*(..)$" ' Pick up the last 2 characters
      myRegexp.Global = True
      myRegexp.IgnoreCase = True

      Dim found
      Set found = myRegexp.test(inputString)
      if  found
         strTEXT = RegExp.$1
      End If
0
 
LVL 1

Author Comment

by:orionpm
ID: 16976566
This is giving me an 'invalid character' on the .$1...
0
 
LVL 1

Author Comment

by:orionpm
ID: 16976645
Thanks for your assistance on this... I had to make a slight variation to .$1 for VBScript... This worked when I put this in quotes in my replace string...

strTEXT = objRegExp.Replace(strContents,"$1")
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Six Sigma Control Plans

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question