I have the following XML tag
<list message="2 < 3">
I want to replace the <
in the text with <
Need a regex to match <
if it doesn't appear at the start of line.
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.
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.
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.
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.
Most likely you can do this using lookbehind:
/(?<!^)</
see: http://www.regular-expressions.info/lookaround.html
[^<]+
= one or more characters that are not <
<
= the < you're looking for
replace:
([^<]+)<
with:
$1<
The dot '.' means "any value"
.<
Anyway, I suppose you don't want whitespaces, either. If so, then
\S\s*<
This would give you "<" after the first instance:
[^<]<
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With