Solved

Regular Expression

Posted on 2006-06-23
10
417 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countX 22 70
stringclean challenge 26 56
control image tags in a string ? 12 61
Please explain: Aspect Oriented Programming 2 53
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

707 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

17 Experts available now in Live!

Get 1:1 Help Now