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
9e1b7ed3
Commit
9e1b7ed3
authored
May 05, 2017
by
zhuyue
Committed by
GitHub
May 05, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update RegexSelector.java
parent
975adf70
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
20 deletions
+8
-20
RegexSelector.java
...in/java/us/codecraft/webmagic/selector/RegexSelector.java
+8
-20
No files found.
webmagic-core/src/main/java/us/codecraft/webmagic/selector/RegexSelector.java
View file @
9e1b7ed3
...
...
@@ -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,20 +46,6 @@ public class RegexSelector implements Selector {
this
(
regexStr
,
1
);
}
private
boolean
hasGroup
(
String
regexStr
)
{
int
x
=
StringUtils
.
countMatches
(
regexStr
,
"("
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\("
);
int
a
=
StringUtils
.
countMatches
(
regexStr
,
"(?:"
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\(?:"
);
int
b
=
StringUtils
.
countMatches
(
regexStr
,
"(?="
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\(?="
);
int
c
=
StringUtils
.
countMatches
(
regexStr
,
"(?<"
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\(?<"
);
int
d
=
StringUtils
.
countMatches
(
regexStr
,
"(?!"
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\(?!"
);
int
e
=
StringUtils
.
countMatches
(
regexStr
,
"(?#"
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\(?#"
);
if
(
x
==
(
a
+
b
+
c
+
d
+
e
))
{
return
false
;
}
return
true
;
}
@Override
public
String
select
(
String
text
)
{
return
selectGroup
(
text
).
get
(
group
);
...
...
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