Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match not start of line

Tags:

regex

I have the following XML tag

<list message="2 < 3">

I want to replace the < in the text with &lt;

Need a regex to match < if it doesn't appear at the start of line.

like image 696
Zaje Avatar asked Jun 23 '11 08:06

Zaje


People also ask

Should not start with number regex?

First, to negate a character class, you put the ^ inside the brackets, not before them. ^[0-9] means "any digit, at the start of the string"; [^0-9] means "anything except a digit". Second, [^0-9] will match anything that isn't a digit, not just letters and underscores.

How do I specify start and end in regex?

To match the start or the end of a line, we use the following anchors: Caret (^) matches the position before the first character in the string. Dollar ($) matches the position right after the last character in the string.

How do you search for a regex pattern at the beginning of a string?

The meta character “^” matches the beginning of a particular string i.e. it matches the first character of the string. For example, The expression “^\d” matches the string/line starting with a digit. The expression “^[a-z]” matches the string/line starting with a lower case alphabet.

How do you match a line in regex?

To expand the regex to match a complete line, add ‹ . * › at both ends. The dot-asterisk sequences match zero or more characters within the current line.


4 Answers

Most likely you can do this using lookbehind:

/(?<!^)</

see: http://www.regular-expressions.info/lookaround.html

like image 55
morphles Avatar answered Oct 10 '22 12:10

morphles


[^<]+ = one or more characters that are not <

< = the < you're looking for

replace:

([^<]+)<

with:

$1&lt;
like image 27
duncan Avatar answered Oct 10 '22 11:10

duncan


The dot '.' means "any value"

.<

Anyway, I suppose you don't want whitespaces, either. If so, then

\S\s*<
like image 29
SJuan76 Avatar answered Oct 10 '22 10:10

SJuan76


This would give you "<" after the first instance:

[^<]<
like image 36
Aziz Shaikh Avatar answered Oct 10 '22 10:10

Aziz Shaikh