Solved

Regular Expression

Posted on 2006-06-23
10
423 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …
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…

679 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