Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expressions: how to accept any symbol

Tags:

I want to replace any content in my text file in between symbols < and >

What's the regular expression to accept any symbol ? I've currently:

fields[i] = fields[i].replaceAll("\\<[a-z0-9_-]*\\>", ""); 

But it works only for letters and numbers, if there is a symbol in between < and >, the string is not replaced.

thanks

like image 390
aneuryzm Avatar asked Feb 20 '11 16:02

aneuryzm


People also ask

How do I allow special characters in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .

How do I allow all items in regex?

Throw in an * (asterisk), and it will match everything. Read more. \s (whitespace metacharacter) will match any whitespace character (space; tab; line break; ...), and \S (opposite of \s ) will match anything that is not a whitespace character.

What does ?! Mean in regex?

It's a negative lookahead, which means that for the expression to match, the part within (?!...) must not match. In this case the regex matches http:// only when it is not followed by the current host name (roughly, see Thilo's comment).

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .


2 Answers

To accept any symbol, .* should do the trick

like image 150
Intrepidd Avatar answered Dec 01 '22 09:12

Intrepidd


Try this [^\>]* (any character that isn't >)

like image 24
xanatos Avatar answered Dec 01 '22 08:12

xanatos