Redian新闻
>
A regular expression question
avatar
A regular expression question# Java - 爪哇娇娃
g*y
1
Given a String, replace | with || if:
(1) | is not within '(single-quote)
(2) | is not followed by | (keep || as is)
I think about the regular expression in Java, seems very difficult to describe
. Finally, I end up with writing a function to do it.
Anyone can solve?
avatar
m*t
2

describe
Do you _have_ to do it with a regular expression?

【在 g**********y 的大作中提到】
: Given a String, replace | with || if:
: (1) | is not within '(single-quote)
: (2) | is not followed by | (keep || as is)
: I think about the regular expression in Java, seems very difficult to describe
: . Finally, I end up with writing a function to do it.
: Anyone can solve?

avatar
g*y
3

No, as I said, I already got around with a Java function. But I am curious to
know: is it possible to do in regular expression?
Could be an interview question.

【在 m******t 的大作中提到】
:
: describe
: Do you _have_ to do it with a regular expression?

avatar
R*r
4
Then it's not related to java, hehe.

to

【在 g**********y 的大作中提到】
:
: No, as I said, I already got around with a Java function. But I am curious to
: know: is it possible to do in regular expression?
: Could be an interview question.

avatar
t*t
5
the following may be a perl solution if you wanna also
change "|||" "|||||||" to ||
s{\|+}{||}g unless /\'/../\'/

describe

【在 g**********y 的大作中提到】
: Given a String, replace | with || if:
: (1) | is not within '(single-quote)
: (2) | is not followed by | (keep || as is)
: I think about the regular expression in Java, seems very difficult to describe
: . Finally, I end up with writing a function to do it.
: Anyone can solve?

avatar
g*y
6
This is not my question. An example of my question is:
Original: "testStr==null | testStr=='ls | wc -l'"
Result: "testStr==null || testStr=='ls | wc -l'"
I'd like to see Java solution, means using String.replaceAll(), just one line
to solve it.

【在 t*****t 的大作中提到】
: the following may be a perl solution if you wanna also
: change "|||" "|||||||" to ||
: s{\|+}{||}g unless /\'/../\'/
:
: describe

avatar
t*t
7
why not try:
if(sth) String.replaceAll();
that can make your life easier and your code more human-readable.
Anyway, I dont know Java and also the solution I posted doesnot work
coz ".." operates only line by line. while IMO, it's much easier
to figure a conditional statement instead of getting a
lookahead/lookbehind regex. You know, it could take long time
to parse your input when backtracking a non-matching string. So
I won't use regex in case I can get an alternative solution.
Just my 2 cen

【在 g**********y 的大作中提到】
: This is not my question. An example of my question is:
: Original: "testStr==null | testStr=='ls | wc -l'"
: Result: "testStr==null || testStr=='ls | wc -l'"
: I'd like to see Java solution, means using String.replaceAll(), just one line
: to solve it.

avatar
m*t
8

to
If just for the fun of it... 2 might be easy to achieve with
a negative lookbehind, but I doubt you can do 1 in a regex --
it sounds more complex than any finite automata can handle.

【在 g**********y 的大作中提到】
: This is not my question. An example of my question is:
: Original: "testStr==null | testStr=='ls | wc -l'"
: Result: "testStr==null || testStr=='ls | wc -l'"
: I'd like to see Java solution, means using String.replaceAll(), just one line
: to solve it.

avatar
t*t
9
Hi, Am not sure if this will work on your Java code, say:
PATTERN: \|+([^|']*)(\'.*?\')?
REPLACE: ||$1$2
I assume Java use the same regex engine as Perl, say NFA
then using backreferences is ok.
the perl cmd line I am using here is as following(under WinXP):
perl -pe "s{\|+([^|']*)(\'.*?\')?}{||$1$2}g" in.txt
Good luck.

line

【在 g**********y 的大作中提到】
: This is not my question. An example of my question is:
: Original: "testStr==null | testStr=='ls | wc -l'"
: Result: "testStr==null || testStr=='ls | wc -l'"
: I'd like to see Java solution, means using String.replaceAll(), just one line
: to solve it.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。