Regular expression to match characters only at the beginning of a line

I am trying to work with regular expressions. I have a mainframe file that has several fields. I have a flat file analyzer that distinguishes several types of records based on the first three letters of each line. How to write a regular expression, where the first three letters are "CTR".

Thank you in advance

+44
regex
Apr 01 '11 at 16:24
source share
7 answers

The beginning of a line or the beginning of a line?

Beginning and end of line

/^CTR.*$/ 

/ = delimiter
^ = start of line
CTR = literal CTR $ = end of line
.* = zero or more of any character except a newline

Beginning and end of line

 /^CTR.*$/m 

/ = delimiter
^ = start of line
CTR = literal CTR $ = end of line
.* = zero or more of any character except the newline character
m = enables multi-line mode, this sets the regular expression to treat each line as a string, so ^ and $ will match the beginning and end of the line

In multi-line mode, you can still combine the beginning and end of a line with \A\Z constant anchors

 /\ACTR.*\Z/m 

\A = means start of line
CTR = literal CTR .* = Zero or more of any character except the newline character
\Z = end of line
m = enables multi-line mode

So another way to match the beginning of the line would be this:

 /(\A|\r|\n|\r\n)CTR.*/ 

or

 /(^|\r|\n|\r\n)CTR.*/ 

\r = carriage return / old version of the new Mac OS | \n = line-feed / Unix / Mac OS X newline
\r\n = windows newline

Please note that if you are going to use the backslash \ in some line of the program that supports escaping, for example, double quotes php "" , you need to avoid them first

therefore, to run \r\nCTR.* you would use it as "\\r\\nCTR.*"

+67
Mar 26 '14 at 18:47
source share
 ^CTR 

or

 ^CTR.* 

edit:

To be more clear ... ^CTR will match the beginning of the line and those characters ... if all you want to do is match for the line itself (and there is already a line to use), then all you really are (but if this is so, you might be better off using a function like prefab substr() ... dunno which language you use ...). But if you are trying to match and capture a string, you will need something like .* Or .*$ Or something else, depending on what language / regular expression function you use ...

+13
Apr 01 2018-11-11T00:
source share

Try ^CTR.\* , Which literally means the beginning of a line, CTR, something .

This will be case sensitive , and the case insensitive setting will depend on your programming language or use ^[Cc][Tt][Rr].\* If there is a problem with registering across multiple environments.

+5
Apr 01 2018-11-11T00:
source share
 ^CTR.*$ 

matches a line starting with a CTR .

+3
Apr 01 2018-11-11T00:
source share

You do not know how to apply this to a file on your server, but usually the regular expression corresponding to the beginning of the line will look like this:

 ^CTR 


^ means start of line / line

+2
Apr 01 2018-11-11T00:
source share
  (? i) ^ [\ r \ n] * CTR

 (? i) - case insensitive - Remove if case sensitive.
 [\ r \ n] - ignore space and new lines
 * - 0 or more times the same
 CTR - your starts with string.

+1
Oct. 15 '15 at 10:24
source share

There are ambiguities in the question.

What is your input line? Is this the whole file? Or is it one line at a time? Some of the answers suggest the latter. I want to answer the first.

What would you like to return from your regular expression? The fact that you want true / false about whether a match was made? Or do you want to extract the entire line that begins with a CTR? I will answer that you only need a true / false match.

To do this, we just need to determine whether CTR occurs at the beginning of the file or immediately after a new line.

 /(?:^|\n)CTR/ 
0
May 16 '17 at 3:45 a.m.
source share



All Articles