Menu
  • HOME
  • TAGS

Regex to create a group from an entire line, or just up to a given token

javascript,regex,lookahead,end-of-line

Just add an end of the line anchor inside the positive lookahead assertion. ^(.*?)\s*(?=[*\[]|$) DEMO...

java regex $ metacharacter

java,regex,end-of-line

You must enable multiline mode, then $ will match both, the end of a line and the end of the input: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#MULTILINE i.e.: Pattern.compile("$",Pattern.MULTILINE); You can also use the flag expression (?m), i.e.: Pattern.compile("(?m)$"); The oracle docs you cite are really quite imprecise here. The documentation of the pattern class...

End of line character / Carriage return

c,file,text,carriage-return,end-of-line

If you are opening a text file and want newline conversions to take place, open the file in "r" mode instead of "rb" FILE *fp = fopen(fname, "r"); this will open in text mode instead of binary mode, which is what you want for text files. On linux there won't...

“Optional” end of line $ literal (non greedy)

regex,non-greedy,end-of-line

You are really close: all you need is replacing ., which matches any character, with [^;], like this: ([^;]+\s*)\s*(?:(\(\s*[xX\*]\s*\d+\s*\))|$) Now your expression would capture the first part if (x10) is present (demo), or the second part if it is missing (demo)....

Python regex: How to match a string at the end of a line in a file?

python,regex,end-of-line

You need to use re.search function, since match tries to match the string from the beginning. var1 = 'network1' print(re.search(r'.*(\s+'+ var1 + r':)', line).group(1)) Example: >>> import re >>> s = 'foo network1: network1:' >>> var1 = 'network1' >>> print(re.search(r'.*(\s+'+ var1 + r':)', s).group(1)) network1: >>> print(re.search(r'.*(..\s+'+ var1 + r':)',...