如何在java中连续限制1个以上的&符号

时间:2015-07-06 11:57:28

标签: java regex pattern-matching

我的字符串应包含one name,然后&,然后second name。 并且不应发生两个连续的&。最后一个字符也不应该是&

我尝试过以下代码,但这不起作用

System.out.println("45345&&345".matches("(^(?!&)(?!.*&&)[0-9&]+(?<!&))$"));

正则表达式为(^(?!&)(?!.*&&)[0-9&]+(?<!&))$

但它不起作用!! 你能帮我吗?

2 个答案:

答案 0 :(得分:3)

String sa = "santanu&sahoo&";
System.out.println(!sa.contains("&&")?sa.matches(".*([a-zA-Z0-9]+)(&)([a-zA-Z0-9]+).*&$"):"False");

答案 1 :(得分:2)

只有一个&在线,并且不会以&开头或结尾:

^[^&]+&[^&]+$

可以包含多个&,但不能包含连续的&,而不是以^(?:[^&]+&)+[^&]+$ 开头或结尾:

add code in xml file
<org.lucasr.twowayview.TwoWayView
                    android:id="@+id/lvItems"
                    style="@style/TwoWayView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_toLeftOf="@+id/linearLayout3"
                    android:cacheColorHint="@android:color/transparent"
                    android:divider="@android:color/transparent"
                    android:drawSelectorOnTop="false"
                    android:padding="10dp" >
                </org.lucasr.twowayview.TwoWayView>