

Using line-oriented tools to process XML is also fraught with potential issues relating to the placement of newlines and the relative placement of node attributes within lines (note that is the same as ). Using sed to modify XML is difficult, especially if you're trying to use a Perl-like regular expression that sed simply does not support. The regular expression suggested by the OP used a couple of non-standard extensions, so it didn't work with sed, but you don't need them: I use sed -i to replace the port value in a xml file, But I don't know how escape the regex, my regex is below. $ xmlstarlet ed -u -v "$NEWPORT" file.xml The selected attributes will be set to $NEWPORT (this is assumed to be a port number). The XPATH expression means "the value attribute belonging to any property node whose name attribute is port".
#Regex escape update#
Using XMLStarlet on the XML document file.xml to update the value of the value attribute of all property nodes whose name attribute is port: xmlstarlet ed -u -v "$NEWPORT" file.xml An exception is the non-greedy match: In some cases you need a different concept without them. Here a simple whitespace does the jobĬonclusion: Almost all reg-ex extensions can easily be avoided, maybe giving a little lengthy pattern or harder to read, but often without disadvantage. I use sed -i to replace the port value in a xml file,īut I don't know how escape the regex, my regex is below. The regular expression suggested by the OP used a couple of non-standard extensions, so it didn't work with sed, but you don't need them: The OP wrote it works, so this time as an answer: sed "s/\(name=\"port\" *value=\"\)*/\1$NEWPORT/g" config.xml ]/ - Digit allowed in a hexadecimal number (i.e.I wrote this in a comment because I didn't realize that this is already the solution. ]/ - Whitespace character (, newline, carriage return, etc.) ]/ - Like, but includes the space character ]/ - Non-blank character (excludes spaces, control characters, and similar) For instance, /\d/ matches only the ASCII decimal digits (0-9) whereas /]/ matches any character in the Unicode Nd category.
#Regex escape portable#
They provide a portable alternative to the above, with the added benefit that they encompass non-ASCII characters. POSIX bracket expressions are also similar to character classes. Please take a look at Bug #4044 if using /\W/ with the /i modifier. /m - Any character (the m modifier enables multiline mode) things that would be usually ignored by the regex paraser e.g. The following metacharacters also behave like character classes: Thus we usually do re.escape(regex) to escape things we want to be interpreted literally i.e.


The two can be combined as follows: /z]/ # ( AND ( OR z)) However, character classes also support the & operator which performs set intersection on its arguments. By itself this isn't useful because ] describes the same set as.

match( 'f') #=> #Ī character class may contain another character class. If the first character of a character class is a caret ( ^) the class is inverted: it matches any character except those named. The order in which ranges or individual characters appear inside a character class is irrelevant. A range can be followed by another range, so is equivalent to. Within a character class the hyphen ( -) is a metacharacter denoting an inclusive range of characters. means a or b, as opposed to /ab/ which means a followed by b. match( "Go to 東京都")Ī character class is delimited with square brackets ( ) and lists characters that may appear at that point in the match. Regexps are created using the /./ and %r/. A Regexp holds a regular expression, used to match a pattern against strings.
