Solved

Regular Expression

Posted on 2006-06-23
10
421 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now