Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webmagic
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
沈俊林
webmagic
Commits
b6b991a0
Commit
b6b991a0
authored
Jun 03, 2017
by
Yihua Huang
Committed by
GitHub
Jun 03, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #556 from zhuyuesut/master
增加对零宽断言的支持
parents
f02f469c
9e1b7ed3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
32 deletions
+10
-32
RegexSelector.java
...in/java/us/codecraft/webmagic/selector/RegexSelector.java
+8
-30
RegexSelectorTest.java
...ava/us/codecraft/webmagic/selector/RegexSelectorTest.java
+2
-2
No files found.
webmagic-core/src/main/java/us/codecraft/webmagic/selector/RegexSelector.java
View file @
b6b991a0
...
...
@@ -26,14 +26,16 @@ public class RegexSelector implements Selector {
if
(
StringUtils
.
isBlank
(
regexStr
))
{
throw
new
IllegalArgumentException
(
"regex must not be empty"
);
}
// Check bracket for regex group. Add default group 1 if there is no group.
// Only check if there exists the valid left parenthesis, leave regexp validation for Pattern.
if
(
!
hasGroup
(
regexStr
)
){
regexStr
=
"("
+
regexStr
+
")"
;
}
this
.
regexStr
=
regexStr
;
try
{
regex
=
Pattern
.
compile
(
regexStr
,
Pattern
.
DOTALL
|
Pattern
.
CASE_INSENSITIVE
);
// Check bracket for regex group. Add default group 1 if there is no group.
// Only check if there exists the valid left parenthesis, leave regexp validation for Pattern.
if
(
regex
.
matcher
(
""
).
groupCount
()
==
0
){
regexStr
=
"("
+
regexStr
+
")"
;
regex
=
Pattern
.
compile
(
regexStr
,
Pattern
.
DOTALL
|
Pattern
.
CASE_INSENSITIVE
);
}
this
.
regexStr
=
regexStr
;
}
catch
(
PatternSyntaxException
e
)
{
throw
new
IllegalArgumentException
(
"invalid regex"
,
e
);
}
...
...
@@ -44,30 +46,6 @@ public class RegexSelector implements Selector {
this
(
regexStr
,
1
);
}
private
boolean
hasGroup
(
String
regexStr
)
{
if
(
StringUtils
.
countMatches
(
regexStr
,
"("
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\("
)
==
StringUtils
.
countMatches
(
regexStr
,
"(?:"
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\(?:"
)){
return
false
;
}
if
(
StringUtils
.
countMatches
(
regexStr
,
"("
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\("
)
==
StringUtils
.
countMatches
(
regexStr
,
"(?="
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\(?="
)
)
{
return
false
;
}
if
(
StringUtils
.
countMatches
(
regexStr
,
"("
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\("
)
==
StringUtils
.
countMatches
(
regexStr
,
"(?<"
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\(?<"
)
)
{
return
false
;
}
if
(
StringUtils
.
countMatches
(
regexStr
,
"("
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\("
)
==
StringUtils
.
countMatches
(
regexStr
,
"(?!"
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\(?!"
)
)
{
return
false
;
}
if
(
StringUtils
.
countMatches
(
regexStr
,
"("
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\("
)
==
StringUtils
.
countMatches
(
regexStr
,
"(?#"
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\(?#"
)
)
{
return
false
;
}
return
true
;
}
@Override
public
String
select
(
String
text
)
{
return
selectGroup
(
text
).
get
(
group
);
...
...
webmagic-core/src/test/java/us/codecraft/webmagic/selector/RegexSelectorTest.java
View file @
b6b991a0
...
...
@@ -25,8 +25,8 @@ public class RegexSelectorTest {
@Test
public
void
testRegexWithZeroWidthAssertions
()
{
String
regex
=
"^.*(?=\\?)"
;
String
source
=
"hello world?xx
xx
"
;
String
regex
=
"^.*(?=\\?)
(?!\\?yy)
"
;
String
source
=
"hello world?xx
?yy
"
;
RegexSelector
regexSelector
=
new
RegexSelector
(
regex
);
String
select
=
regexSelector
.
select
(
source
);
Assertions
.
assertThat
(
select
).
isEqualTo
(
"hello world"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment